如何选择只与LINQ最高日纪录 [英] How to select only the records with the highest date in LINQ

查看:147
本文介绍了如何选择只与LINQ最高日纪录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,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();

这篇关于如何选择只与LINQ最高日纪录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆