开发者

Select all customers and their last order Linq to SQL

开发者 https://www.devze.com 2023-01-06 07:26 出处:网络
hei, Using Linq to SQL (ala NorthWind database for example): How to select all customers and the latest order for each of them. The customers who have not placed开发者_运维技巧 order should be also

hei,

Using Linq to SQL (ala NorthWind database for example): How to select all customers and the latest order for each of them. The customers who have not placed开发者_运维技巧 order should be also in the result. The last order could be by ID (ID is incremental) or Timestamp (DateTime field).

similar to this SQL Statement Help - Select latest Order for each Customer but done in LINQ.

thanks


Assuming there's a foreign-key relationship between Customers & Orders, something like this sghould work:

from c in db.Customers
select new 
       { 
         Customer = c,
         LastOrder = c.Orders.OrderByDescending(o=>o.Timestamp).First();
       };
0

精彩评论

暂无评论...
验证码 换一张
取 消