【问题标题】:GroupBy with linq entity pocoGroupBy 与 linq 实体 poco
【发布时间】:2019-05-29 14:14:36
【问题描述】:

我有这个实体

public class Counter
    {
        public int DocEntry { get; set; }
        public int LineId { get; set; }
        public string Item { get; set; }
        public decimal Quantity { get; set; }
        public string FromWarehouse { get; set; }
        public string ToWarehouse { get; set; }
        public virtual List<Batch> batchs { get; set; }
    }

    public class Batch
    {
      public string BatchNumber { get; set; }

        public decimal Quantity { get; set; }
    }

我有列表计数,我应该根据 Item 值、FromWarehouse、ToWarehouse 对列表的元素进行分组,结果应该按总数量和 Batch 合并列表分组,我尝试使用循环计数器列表foreach 方法并在后续迭代中将第一个元素插入到新列表中,如果当前行反映了列表中已有的值-th 行,否则我添加了一个新行,这种方法似乎太复杂了,对 linq 不是很专家,或者无论如何有更简单的方法来管理组?

这是我的方法:

public static List<Counter> GroupBy(List<Counter> list)
        {
            List<Counter> rows = new List<Counter>();
            int i = 0;
            foreach (Counter elem in list)
            {
                if (list.First() == elem)
                {
                    rows.Add(elem);
                    i++;
                }
                else
                {

                    if (elem.Item == rows.ElementAt(i).Item &&
                        elem.FromWarehouse == rows.ElementAt(i).FromWarehouse &&
                        elem.ToWarehouse == rows.ElementAt(i).ToWarehouse)
                    {
                        rows.First().Quantity += elem.Quantity;
                        rows.First().batchs.Add(elem.batchs.First());
                    }
                    else
                    {
                        rows.Add(elem);
                        i++;
                    }
                }
            }

            return rows;
        }

这是解决方案:

public static List<Counter> GroupBy(List<Counter> list)
        {
            List<Counter> rows = new List<Counter>();

            foreach (Counter elem in list)
            {
                if (rows.Any(x=>x.Item == elem.Item &&
                        x.FromWarehouse == elem.FromWarehouse &&
                        x.ToWarehouse == elem.ToWarehouse))
                {
                    rows.First().Quantity += elem.Quantity;
                    rows.First().batchs.Add(elem.batchs.First());
                }
                else
                {
                    rows.Add(elem);
                }
            }

            return rows;
        }

【问题讨论】:

  • 你尝试了什么?
  • 我只是编辑我的答案

标签: c# list linq group-by


【解决方案1】:

让我们试着写Linq,一步一步来。

“我有列表count”:

   List<Counter> count = ...

“我应该根据Item 值、FromWarehouseToWarehouse 对列表的元素进行分组

   var result = count
     .GroupBy(item => new {
        item.Item,
        item.FromWarehouse
        item.ToWarehouse 
      })

"result 应该与 summed quantityBatch 列表 merged" 分组,即你应该在 Aggregate 项目每个 chunk

   var result = count  
     .GroupBy(item => new {
        item.Item,
        item.FromWarehouse
        item.ToWarehouse 
      })
     .Select(chunk => new {
        key = chunk.Key,
        summary = chunk.Aggregate(
          Tuple.Create(0m, new List<Batch>()),                      // initial empty
          (s, a) => Tuple.Create(s.Item1 + a.Quantity,              // add
                                 s.Item2.Concat(a.batchs).ToList()) // merge
      })

最后,让我们用更方便(可读)的格式来表示result

   var result = count  
     .GroupBy(item => new {
        item.Item,
        item.FromWarehouse
        item.ToWarehouse 
      })
     .Select(chunk => new {
        key = chunk.Key,
        summary = chunk.Aggregate(
          Tuple.Create(0m, new List<Batch>()),
          (s, a) => Tuple.Create(s.Item1 + a.Quantity, 
                                 s.Item2.Concat(a.batchs).ToList())
      })
     .Select(item => new {
        Item = item.key.Item,
        FromWarehouse = item.key.FromWarehouse,
        ToWarehouse = item.key.ToWarehouse,
        Quantity = item.summary.Item1,
        Batches = item.summary.Item2
      }); // Add ToArray() if you want to materialize

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多