LINQ To Entities: Strange, very strange
From Northwind, if I run this query:
from c in context.Customers.Include("Orders")
where c.CompanyName.StartsWith("An")
let od = (from o in c.Orders
select o.OrderDate).OrderByDescending(o => o).FirstOrDefault()
orderby od descending
select c;
I have the customers of these company:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Now if I do this:
from c in context.Customers.Include("Orders")
where c.CompanyName.StartsWith("An")
orderby (from o in c.Orders
select o.OrderDate).OrderByDescending(o => o).FirstOrDefault() descending
select c;
I have this:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
Now, if I do this:
from c in context.Customers//.Include("Orders")
where c.CompanyName.StartsWith("An")
orderby (from o in c.Orders
select o.OrderDate).OrderByDescending(o => o).FirstOrDefault() descending
select c;
I have this:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
To finish, if I do this:
from c in context.Customers.Include("Orders")
where c.CompanyName.StartsWith("An")
orderby (from o in c.Orders
select o.OrderDate).OrderByDescending(o => o).FirstOrDefault() descending
select c.CompanyName;
I have this:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
I also try to do this:
from c in context.Customers
where c.CompanyName.StartsWith("An")
let orders = c.Orders.OrderByDescending(o => o.OrderDate)
let lastOrderDate = orders.Select(o => o.OrderDate).FirstOrDefault()
orderby lastOrderDate descending
select new { Customer = c, Orders = orders };
I have this result:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Ana Trujillo Emparedados y helados
But without the Orders in the anonymous type:
from c in context.Customers
where c.CompanyName.StartsWith("An")
let orders = c.Orders.OrderByDescending(o => o.OrderDate)
let lastOrderDate = orders.Select(o => o.OrderDate).FirstOrDefault()
orderby lastOrderDate descending
select new { Customer = c };
I have this:
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería