合并重复数据而不影响 LINQ 代码中的其他数据

发布于 2024-10-25 10:20:04 字数 378 浏览 2 评论 0原文

目标:
如果有重复,则应将其合并为一个,例如,如果“_name: a”, _quantity: 3”和“_name: a”, _quantity: 6”应合并为“_name: a”, _quantity: 9”与其他数据重复或不重复的列表

问题: 我有一个包含大量数据的列表,我不知道如何将重复的数据合并到一个单元中+它不应该影响其他单元。如果可能的话,希望所有事情都发生在同一个列表中,而不创建更多列表来实现结果。

List<Sale> myList = GetAllSalesList()


public classs Sale
{
     public string _name;
     public string _quantity;
}

Goal:
If any duplicate, it should be merged into one for instance if "_name: a", _quantity: 3" and "_name: a", _quantity: 6" should be merged into "_name: a", _quantity: 9" in the list with other data with duplicate or not.

Problem:
I have a list with lots of data and I don't know how to merge duplicate data into one unit + it should not affect others. If possible, would like everyting to be happened inte same list without creating more list to acheive the result.

List<Sale> myList = GetAllSalesList()


public classs Sale
{
     public string _name;
     public string _quantity;
}

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

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

发布评论

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

评论(3

独享拥抱 2024-11-01 10:20:04

像这样的东西?

var mergedList = 
myList.GroupBy(x => x._name)
      .Select(g => new Sale{_name = g.Key, _quantity = g.Sum(x => x._quantity)})
      .ToList();

编辑:

我刚刚注意到_quantity是一个字符串,对吗?
如果是,则使用 Sum() 将代码片段更改为:

g.Sum(x => int.Parse(x._quantity)).ToString()

当然,如果 _quantity 表示 int...否则使用 <代码>双精度或十进制等...

Something like this ?

var mergedList = 
myList.GroupBy(x => x._name)
      .Select(g => new Sale{_name = g.Key, _quantity = g.Sum(x => x._quantity)})
      .ToList();

EDIT:

I've just noticed that _quantity is a string, is that correct ?
If it is, change the piece of code using Sum(), to:

g.Sum(x => int.Parse(x._quantity)).ToString()

of course if _quantity represents an int... otherwise use double or decimal etc...

久隐师 2024-11-01 10:20:04

您可以使用分组来完成此操作

var query = from sales in list
            group sale by _name into grouped
            select new Sale
            {
               _name = grouped.Key,
               _quantity = grouped.Sum(x => x._quantity)
            }; 

var groupedList = query.ToList();

。有关分组的更多信息,请参阅第 101 页中的实际示例 - http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

注意 - 这个答案假设 _quantity 实际上是一个 int 并且整数溢出不是问题

You could do this by using Grouping

var query = from sales in list
            group sale by _name into grouped
            select new Sale
            {
               _name = grouped.Key,
               _quantity = grouped.Sum(x => x._quantity)
            }; 

var groupedList = query.ToList();

For more on grouping, please see the practical examples in the 101 page - http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Note - this answer assumes that _quantity is actually an int and that integer overflow is not a concern

蹲在坟头点根烟 2024-11-01 10:20:04
var aggregateList = 
    myList.GroupBy(i => i._name)
          .Select(g => new Sale
         {
             _name = g.Key, 
             _quantity = g.Sum(x => Convert.ToInt64(x._quantity)).ToString()
         })
         .ToList();
var aggregateList = 
    myList.GroupBy(i => i._name)
          .Select(g => new Sale
         {
             _name = g.Key, 
             _quantity = g.Sum(x => Convert.ToInt64(x._quantity)).ToString()
         })
         .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文