Bas*_*sen 114 .net c# linq linq-to-sql
我有一个表'lasttraces',带有以下字段.
Id, AccountId, Version, DownloadNo, Date
Run Code Online (Sandbox Code Playgroud)
数据如下所示:
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
Run Code Online (Sandbox Code Playgroud)
在LINQ to SQL中,我怎样才能获得每个AccountId(具有最高日期的那个)的最后一个lasttrace?
Meh*_*ari 224
如果您只想要每个帐户的最后日期,请使用以下内容:
var q = from n in table
group n by n.AccountId into g
select new {AccountId = g.Key, Date = g.Max(t=>t.Date)};
Run Code Online (Sandbox Code Playgroud)
如果你想要整个记录:
var q = from n in table
group n by n.AccountId into g
select g.OrderByDescending(t=>t.Date).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
Dev*_*per 47
这是一个简单的方法
var lastPlayerControlCommand = this.ObjectContext.PlayerControlCommands
.Where(c => c.PlayerID == player.ID)
.OrderByDescending(t=>t.CreationTime)
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
还看看这个很棒的LINQ地方 - LINQ to SQL Samples
Bob*_*ang 27
如果你想要整个记录,这里是一个lambda方式:
var q = _context
.lasttraces
.GroupBy(s => s.AccountId)
.Select(s => s.OrderByDescending(x => x.Date).FirstOrDefault());
Run Code Online (Sandbox Code Playgroud)
可能是这样的:
var qry = from t in db.Lasttraces
group t by t.AccountId into g
orderby t.Date
select new { g.AccountId, Date = g.Max(e => e.Date) };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176207 次 |
| 最近记录: |