本文介绍了LINQ:Groupby和Last的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是LINQ的新手.

我有下表:

ID  Field1  Field2      Field3
1   aaaa    20/01/2014  10
2   aaaa    21/01/2014  3
3   aaaa    25/01/2014  10
4   bbbb    01/01/2014  90
5   bbbb    03/01/2014  1
6   bbbb    31/01/2014  5

我想按Field1分组并抓住每个分组的最后一行.与此等效的SQL查询是:

I want to group by Field1 and grab the last line of each group.The SQL Query equivalent to this is:

SELECT Field1, Last(Field2) AS LastOfField2, Last(Field3) AS LastOfField3
FROM Table1
GROUP BY Field1

如何在Linq上实现这一目标?

How this can be achieved on Linq?

推荐答案

var result = from p in Table1
group p by p.Field1 into grp
select grp.OrderByDescending(g=>g.Field2).First();

这篇关于LINQ:Groupby和Last的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:05