Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Unassigned: Wrong entity returned when using orderby, let and FirstOrDefault [1196]

$
0
0
I have attached a console application to demonstrate the problem:

I have a list of entities: A.

Each entity A has 2 separate collections of entity B associated with it Bs1, and Bs2.

The query should return a list of the first B in the list: Bs2

```
from a in context.As
select a.Bs2.FirstOrDefault()
```

so far so good (this works)

I then throw in a let statement:

```
from a in context.As
let b1First = a.Bs1.FirstOrDefault()
select a.Bs2.FirstOrDefault()
```

still good (the let statement is returning the first B in the list Bs1, though admittedly in this example doing nothing with it)

finally, I order the list:

```
from a in context.As
orderby a.Id
let b1First = a.Bs1.FirstOrDefault()
select a.Bs2.FirstOrDefault()
```

Now there is a problem.. In my attached example, there should be no nulls returned... every A has at least one B in the collection Bs2.. but I get 2 nulls! seemingly it is returning null when there is no B in the list Bs1.. but that should not be relevant here

Hope this is enough information for you

Martin


Comments: Please note, the attached program is actually _working_ because the orderby clause is commented out. Please remove the comment to see how the program breaks

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>