集团通过LINQ查询 [英] group by in LINQ query

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

问题描述

我有一个数据表,其中我想上执行GROUPBY查询。

I've got a DataTable where i want to perform a GroupBy query on.

year    url      type     id
=============================
        someurl  image    0
2003             date     0
        someurl  image    1
2009             date     1

我已经成功地组对我的ID,但我不能够选择年和地址栏。

I've managed to group on my id, but I am not able to select the columns "year" and "Url".

这是我的查询:

var query = from row in table.AsEnumerable()
            let uri = row["uri"]
            group row by row.Field<int>("id") into grp
            orderby grp.Key
            select new
            {
                ID = grp.Key,
            };

我该如何选择栏目一年网址?

How can i select the columns year and url?

在此先感谢。

推荐答案

当你按在LINQ你在你的案件组(GRP)是一个枚举包含组中所有的条目(在你的案件数据行)。然后,您可以的foreach中的条目或使用聚合功能,如最大值,首先,等从枚举提取单个值

When you group by in LINQ your group (grp) in your case is an enumerable containing all the entries in a group (data rows in your case). You can then either foreach the entries or use aggregate functions like Max, First, etc. to extract a single value from the enumerable

这篇关于集团通过LINQ查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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