将多个收集项目合并为一个

时间:2019-08-13 13:43:22

标签: c#

我有一个物品清单。我想遍历列表并合并具有相同CN8,MSConsDestCode和countryOfOriginCode的所有成员。

netMass也应该是合并项目的netMass属性的小计。

public class Item
    {
        public int CN8 { get; internal set; }
        public string goodsDescription { get; internal set; }
        public string MSConsDestCode { get; internal set; }
        public string countryOfOriginCode { get; internal set; }
        public decimal netMass { get; internal set; }
    }

从此:

var data = new List<Item>
{
    new Item {CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  },
    new Item {CN8 = 123456789, goodsDescription = "blah1", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  }
    new Item {CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1  }
},

我正在寻找这样的结果: (前两项合并,因为它们具有相同的CN8,MSConsDestCode和countryofOriginCode,最后一项与上一个列表相同)/

{CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 2  }
{CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1  }

注意:goodsDescription可以是来自任何合并元素的随机名称。

我有一种感觉,我应该尝试使用LINQ,但是由于我不太熟悉它,所以我要求你们引导我朝着正确的方向前进。

2 个答案:

答案 0 :(得分:-1)

您确实可以使用Linq实现此目的:

var data = new List<Item>
{
  new Item {CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  },
  new Item {CN8 = 123456789, goodsDescription = "blah1", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  },
  new Item {CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1  }
};

var combined = data.GroupBy(x => new {x.CN8, x.MSConsDestCode, x.countryOfOriginCode})
                   .Select(x => new Item
                     {
                       CN8 = x.Key.CN8,
                       goodsDescription = x.First().goodsDescription,
                       MSConsDestCode = x.Key.MSConsDestCode,
                       countryOfOriginCode = x.Key.countryOfOriginCode,
                       netMass = x.Sum(v => v.netMass)
                     });

答案 1 :(得分:-2)

您要分组依据,然后选择:

using System;
using System.Collections.Generic;
using System.Linq;

namespace so
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new List<Item>
            {
                new Item {CN8 = 123456789, goodsDescription = "blah0", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  },
                new Item {CN8 = 123456789, goodsDescription = "blah1", MSConsDestCode = "DE", countryOfOriginCode = "CN", netMass = 1  },
                new Item {CN8 = 123456789, goodsDescription = "blah2", MSConsDestCode = "GB", countryOfOriginCode = "IN", netMass = 1  }
            };

            // Here is your query
            var groupedData = data.GroupBy(di => new {di.CN8, di.MSConsDestCode, di.countryOfOriginCode}).Select(group => new Item{ netMass = group.Sum(gi => gi.netMass),goodsDescription = group.First().goodsDescription, CN8=group.Key.CN8, MSConsDestCode=group.Key.MSConsDestCode, countryOfOriginCode=group.Key.countryOfOriginCode} );
        }
    }

    public class Item
    {
        public int CN8 { get; internal set; }
        public string goodsDescription { get; internal set; }
        public string MSConsDestCode { get; internal set; }
        public string countryOfOriginCode { get; internal set; }
        public decimal netMass { get; internal set; }
    }
}

请注意,这里我选择的是第一个组描述,但是鉴于您在原始问题中说的是“随机”元素,因此我认为选择第一个是更好的主意。