如何在LINQ中仅选择日期最高的记录 [英] How to select only the records with the highest date in LINQ
本文介绍了如何在LINQ中仅选择日期最高的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表lasttraces",其中包含以下字段.
I have a table, 'lasttraces', with the following fields.
Id, AccountId, Version, DownloadNo, Date
数据如下:
28092|15240000|1.0.7.1782|2009040004731|2009-01-20 13:10:22.000
28094|61615000|1.0.7.1782|2009040007696|2009-01-20 13:11:38.000
28095|95317000|1.0.7.1782|2009040007695|2009-01-20 13:10:18.000
28101|15240000|1.0.7.1782|2009040004740|2009-01-20 14:10:22.000
28103|61615000|1.0.7.1782|2009040007690|2009-01-20 14:11:38.000
28104|95317000|1.0.7.1782|2009040007710|2009-01-20 14:10:18.000
我如何才能在 LINQ to SQL 中只获取最后一条记录每个 AccountId(日期最高的那个)?
How can I, in LINQ to SQL, only get the last lasttrace of every AccountId (the one with the highest date)?
推荐答案
如果您只想要每个帐户的最后日期,您可以使用这个:
If you just want the last date for each account, you'd use this:
var q = from n in table
group n by n.AccountId into g
select new {AccountId = g.Key, Date = g.Max(t=>t.Date)};
如果你想要整个记录:
var q = from n in table
group n by n.AccountId into g
select g.OrderByDescending(t=>t.Date).FirstOrDefault();
这篇关于如何在LINQ中仅选择日期最高的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文