集团通过LINQ查询集团、LINQ

2023-09-04 04:38:52 作者:偏爱成痛

我有一个数据表,其中我想上执行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