LINQ 查询中的分组依据

发布于 2024-11-06 07:15:36 字数 602 浏览 0 评论 0原文

我有一个 DataTable,我想在其中执行 GroupBy 查询。

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

我已成功按 ID 进行分组,但无法选择“年份”和“网址”列。

这是我的查询:

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,
            };

如何选择年份和网址列?

提前致谢。

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

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

This is my query:

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?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-11-13 07:15:36

当您在 LINQ 中进行分组时,您的案例中的组 (grp) 是一个包含组中所有条目(案例中的数据行)的枚举。然后,您可以 foreach 条目或使用 Max、First 等聚合函数从可枚举中提取单个值

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

花海 2024-11-13 07:15:36

您可以按多个值进行分组。在您的案例中,按 id、年份和 url 分组
检查此处

    List<WebResource> webResources = new List<WebResource>();
    webResources.Add(new WebResource() { Id = 1, Type = "Image", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 1, Type = "Text", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 3, Type = "Image", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 1, Type = "Text", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 2, Type = "Image", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 3, Type = "Text", Url = "url2", Year = 2002 });
    webResources.Add(new WebResource() { Id = 1, Type = "Image", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 2, Type = "Text", Url = "url2", Year = 2002 });

    var output = webResources.GroupBy(webResource => new {webResource.Id, webResource.Year, webResource.Url })
        .Select(group => new {Key = group.Key, Count = group.Count()});

    foreach (var webResource in output)
    {
        Console.WriteLine("Id: " + webResource.Key.Id +" Url:" + webResource.Key.Url + " Year:" + webResource.Key.Year + "  Count:" + webResource.Count);
    }

You can group by multiple values. In your case group by id, year and url
Check here

    List<WebResource> webResources = new List<WebResource>();
    webResources.Add(new WebResource() { Id = 1, Type = "Image", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 1, Type = "Text", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 3, Type = "Image", Url = "url1", Year = 2001 });
    webResources.Add(new WebResource() { Id = 1, Type = "Text", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 2, Type = "Image", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 3, Type = "Text", Url = "url2", Year = 2002 });
    webResources.Add(new WebResource() { Id = 1, Type = "Image", Url = "url1", Year = 2002 });
    webResources.Add(new WebResource() { Id = 2, Type = "Text", Url = "url2", Year = 2002 });

    var output = webResources.GroupBy(webResource => new {webResource.Id, webResource.Year, webResource.Url })
        .Select(group => new {Key = group.Key, Count = group.Count()});

    foreach (var webResource in output)
    {
        Console.WriteLine("Id: " + webResource.Key.Id +" Url:" + webResource.Key.Url + " Year:" + webResource.Key.Year + "  Count:" + webResource.Count);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文