如何通过多个字段与linq分组和订购

发布于 2025-01-25 22:13:51 字数 1211 浏览 4 评论 0原文

我有一个带有4个字段,ID,Store,Sku和Qty的数据表(DTRESULT)。不幸的是,我想删除的数据词中有很多重复项(数量是差异,但其他字段是相同的)。

我想按ID ASC,Store ASC,SKU ASC和ID,Store和Sku对数据进行排序,以便我有唯一记录的列表。

理想情况下,我想通过查询结果覆盖现有的数据表,而QTY的所有内容都可以为0。我有这种排序,目前我将其放入一个新的数据表中:

var dtUniqueResults = dtResult.AsEnumerable()
    .OrderBy(r => r.Field<string>("id"))
    .ThenBy(r => r.Field<string>("store"))
    .ThenBy(r => r.Field<string>("sku"))
    .CopyToDataTable();

我不明白如何与Linq分组。我想我需要添加类似的东西,但它不起作用。

var dtUniqueResults = dtResult.AsEnumerable()
    .GroupBy(n => n.Field<string>("id"),
             n => n.Field<string>("store"),
             n => n.Field<string>("sku")
    )
    .OrderBy(r => r.Field<string>("id"))
    .ThenBy(r => r.Field<string>("store"))
    .ThenBy(r => r.Field<string>("sku"))
    .CopyToDataTable();

我读了很多帖子,我看到了几种做到这一点的方法。但是,似乎最多建议的两个是这些,但是它们似乎是如此不同,这使我更加困惑。

GroupBy( x => new { x.Column1, x.Column2 })

AND

GroupBy(x=> new { x.Column1, x.Column2 }, (key, group) => new 
{ 
  Key1 = key.Column1,
  Key2 = key.Column2,
  Result = group.ToList() 
});

I have a DataTable (dtResult) with 4 fields, id, store, sku and qty. Unfortunately there are a lot of duplicates in the DataTable that I want to remove (qtys are diff, but other fields are the same).

I want to sort the DataTable by id asc, store asc, sku asc, and group by id, store and sku so I would have a list of unique records.

IDEALLY I would like to overwrite the existing DataTable with the results of the query, and qty can just be 0 for everything. I have the sort, and currently I'm putting it into a new DataTable:

var dtUniqueResults = dtResult.AsEnumerable()
    .OrderBy(r => r.Field<string>("id"))
    .ThenBy(r => r.Field<string>("store"))
    .ThenBy(r => r.Field<string>("sku"))
    .CopyToDataTable();

I don't understand how to group with LINQ. I think I need to add something like this, but it's not working.

var dtUniqueResults = dtResult.AsEnumerable()
    .GroupBy(n => n.Field<string>("id"),
             n => n.Field<string>("store"),
             n => n.Field<string>("sku")
    )
    .OrderBy(r => r.Field<string>("id"))
    .ThenBy(r => r.Field<string>("store"))
    .ThenBy(r => r.Field<string>("sku"))
    .CopyToDataTable();

I've read a lot of posts, and I see several ways of doing it. However it seems the two that are suggested the most are these, but they seem so different it just confuses me more.

GroupBy( x => new { x.Column1, x.Column2 })

AND

GroupBy(x=> new { x.Column1, x.Column2 }, (key, group) => new 
{ 
  Key1 = key.Column1,
  Key2 = key.Column2,
  Result = group.ToList() 
});

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

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

发布评论

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

评论(1

满地尘埃落定 2025-02-01 22:13:51

如果您需要过滤出重复项,请尝试以下查询:

var dtUniqueResults = dtResult.AsEnumerable()
    .GroupBy(n => new 
        { 
            Id = n.Field<string>("id"),
            Store = n.Field<string>("store"),
            Sku = n.Field<string>("sku")
        }
    )
    .SelectMany(g => g.Take(1)) // get from group only one record
    .CopyToDataTable();

If you need to filter out duplicates, try the following query:

var dtUniqueResults = dtResult.AsEnumerable()
    .GroupBy(n => new 
        { 
            Id = n.Field<string>("id"),
            Store = n.Field<string>("store"),
            Sku = n.Field<string>("sku")
        }
    )
    .SelectMany(g => g.Take(1)) // get from group only one record
    .CopyToDataTable();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文