Linq表达式与前1名返回孔行 [英] Linq expression with top 1 returning the hole row

查看:41
本文介绍了Linq表达式与前1名返回孔行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个或多或少像这样的桌子(虽然更多行......)


路径    时间    ID


x          X.1        1


x          X.2        2


x          X.3        3


y          X.1        4


y          x.2       5


y          X.3        6


z          X.2        7


我想要的是让时间排序的顶级路径 - 所有rowdata。所以查询的结果将是:


路径   时间    ID       (以及其他行)


x          X.1        1


y          X.1        3


z          X.2        7


其中Time是最新的时间戳1 ...


我尝试过take(1)加入和分组...但似乎被卡住:-(。任何帮助真的很难。


谢谢!

解决方案

你好,


所以你想获得按路径分组的最小时间行。应用
中描述的方法
this
帖子,这是一种方法:


 

来自表格中的p 
group p by p.Path into grp
let MinTimePerPath = grp.Min(g => g.Time)

来自p in grp
其中p.Time == MinTimePerPath
select p

 


另一种方式是:


来自表格中的p 
group p by p.Path into grp
select grp.OrderBy(t => t.Time).First()


 


问候,


Hi,

I have a table more or less like this (more rows though...)

Path     Time     ID

x          x.1        1

x          x.2        2

x          x.3        3

y          x.1        4

y          x.2        5

y          x.3        6

z          x.2        7

What I would lige is getting the top path distinct ordered by time - all rowdata. so the result of the query would be:

Path     Time     ID       (And the rest of the rows)

x          x.1        1

y          x.1        3

z          x.2        7

where Time being a time stamp 1 the newest ...

I have tried take(1) join and group ... but seem to be stucked :-( . any help really apreciated.

Thanks!

解决方案

Hi,

So you want to get the minimum time row grouped by path. Applying the method described in this post, here's one method:

 

from p in Table
group p by p.Path into grp
let MinTimePerPath = grp.Min ( g=>g.Time )

from p in grp
where p.Time == MinTimePerPath
select p

 

Another way would be:

from p in Table
group p by p.Path into grp
select grp.OrderBy(t=>t.Time).First()

 

Regards,


这篇关于Linq表达式与前1名返回孔行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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