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: The query we create is as follows: ``` SELECT [Project1].[C1] AS [C1] FROM ( SELECT C.[CustomerId] AS [CustomerId], (SELECT COUNT(1) AS [A1] FROM (SELECT TOP (1) O.[OrderId] AS [OrderId] FROM [dbo].[Orders] AS O WHERE C.[CustomerId] = O.[Customer_CustomerId] ) AS [Limit1] CROSS JOIN [dbo].[Products] AS P WHERE C.[CustomerId] = P.[Customer_CustomerId]) AS [C1] FROM [dbo].[Customers] AS C ) AS [Project1] ORDER BY [Project1].[CustomerId] DESC ``` We create CROSS JOIN of Orders for a given customer and products for the given customer. The issue is that if customer has 0 orders, then the result of CROSS JOIN will be empty, and hence products (which we are suppose to count) will be incorrect. SELECT TOP (1) O.[OrderId] AS [OrderId] FROM [dbo].[Orders] AS O WHERE C.[CustomerId] = O.[Customer_CustomerId] Should not be in the query at all, it was declared in a variable but is not used at all in the final projection. Query should look more like this: SELECT [Project1].[C1] AS [C1] FROM ( SELECT [Extent1].[CustomerId] AS [CustomerId], (SELECT COUNT(1) AS [A1] FROM [dbo].[Products] AS [Extent2] WHERE [Extent1].[CustomerId] = [Extent2].[Customer_CustomerId]) AS [C1] FROM [dbo].[Customers] AS [Extent1] ) AS [Project1] ORDER BY [Project1].[CustomerId] DESC
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: The query we create is as follows: ``` SELECT [Project1].[C1] AS [C1] FROM ( SELECT C.[CustomerId] AS [CustomerId], (SELECT COUNT(1) AS [A1] FROM (SELECT TOP (1) O.[OrderId] AS [OrderId] FROM [dbo].[Orders] AS O WHERE C.[CustomerId] = O.[Customer_CustomerId] ) AS [Limit1] CROSS JOIN [dbo].[Products] AS P WHERE C.[CustomerId] = P.[Customer_CustomerId]) AS [C1] FROM [dbo].[Customers] AS C ) AS [Project1] ORDER BY [Project1].[CustomerId] DESC ``` We create CROSS JOIN of Orders for a given customer and products for the given customer. The issue is that if customer has 0 orders, then the result of CROSS JOIN will be empty, and hence products (which we are suppose to count) will be incorrect. SELECT TOP (1) O.[OrderId] AS [OrderId] FROM [dbo].[Orders] AS O WHERE C.[CustomerId] = O.[Customer_CustomerId] Should not be in the query at all, it was declared in a variable but is not used at all in the final projection. Query should look more like this: SELECT [Project1].[C1] AS [C1] FROM ( SELECT [Extent1].[CustomerId] AS [CustomerId], (SELECT COUNT(1) AS [A1] FROM [dbo].[Products] AS [Extent2] WHERE [Extent1].[CustomerId] = [Extent2].[Customer_CustomerId]) AS [C1] FROM [dbo].[Customers] AS [Extent1] ) AS [Project1] ORDER BY [Project1].[CustomerId] DESC