如何选择只与LINQ最高日纪录如何选择、纪录、最高、LINQ

2023-09-02 01:52:43 作者:无法言语的痛、

我有一个表,lasttraces,具有以下字段。

I have a table, 'lasttraces', with the following fields.

Id, AccountId, Version, DownloadNo, Date

中的数据是这样的:

The data looks like this:

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 到  SQL ,只有让每一个的AccountId的最后lasttrace(中具有最高日)?

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();