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 

This entry was posted in 7671, 7674. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>