利用Linq对集合元素合并、去重复处理,多个值进行分组

1、对集合元素合并、去重复处理

 /// <summary>
    /// 商品
    /// </summary>
    public class GoodsInfo
    {
        /// <summary>
        /// 编号
        /// </summary>
        public string GoodsNO { get; set; }
        /// <summary>
        /// 数量
        /// </summary>
        public decimal GoodsNum { get; set; }
        /// </summary>
        /// 描述
        /// </summary>
        public string Desc { get; set; }
        /// </summary>
        /// 描述
        /// </summary>
        public string Other{get;set;}
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<GoodsInfo> list = new List<GoodsInfo>();
            list.Add(new GoodsInfo() { GoodsNO = "NO.01", GoodsNum = 3,Desc="test1",Other="xx"});
            list.Add(new GoodsInfo() { GoodsNO = "NO.02", GoodsNum = 1,Desc="test2",Other="xx"});
            list.Add(new GoodsInfo() { GoodsNO = "NO.01", GoodsNum = 2,Desc="test3",Other="xx"});
            list.Add(new GoodsInfo() { GoodsNO = "NO.03", GoodsNum = 4,Desc="test4",Other="xx"});
            list.Add(new GoodsInfo() { GoodsNO = "NO.02", GoodsNum = 2,Desc="test5",Other="xx"});
            
            /* 
            var s = from p in list
                         group p by p.GoodsNO into g
                         select new
                         {
                             GoodsNO = g.Key,//对于多关键字 {GoodsNO,Desc},如可从g.Key.GoodsNo,Desc=g.key.Desc
                             GoodsNum = g.Sum(x => x.GoodsNum),
                             Other=g.First().Other,
                             Desc=string.Join(",", g.Select(t => t.Desc).ToList())
                         };
            */
             var result=list.GroupBy(p => p.GoodsNO ).Select(g => new GoodsInfo { GoodsNO = g.Key,
                             GoodsNum = g.Sum(x => x.GoodsNum),//此注意精度问题,可Math.Round(g.Sum(x => x.GoodsNum,2)
) Other = g.First().Other, Desc = string.Join(",", g.Select(t => t.Desc).ToList())}); result.ToList().ForEach(x => Console.WriteLine(x.GoodsNO + " " + x.GoodsNum + " " + x.Desc +" " +x.Other)); } }

输出结果:

NO.01 5 test1,test3 xx
NO.02 3 test2,test5 xx
NO.03 4 test4 xx

对于 GoodsInfo 有很多字段的情况,需手动写类似 Other=g.First().Other,不知有没有其他方法?

参考引用:

https://blog.csdn.net/qq_24432127/article/details/82789138

https://www.cnblogs.com/yjmyzz/archive/2012/12/18/2823170.html

2、Linq 中按照多个值进行分组(GroupBy)

 
.GroupBy(x => new { x.Age, x.Sex })

group emp by new { emp.Age, emp.Sex } into g


// 实现多key分组的扩展函数版本
var sums = empList
         .GroupBy(x => new { x.Age, x.Sex })
         .Select(group => new {
            Peo = group.Key, Count = group.Count()
         });
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}

// 实现多key分组的lambda版本
var sums2 = from emp in empList
            group emp by new { emp.Age, emp.Sex } into g
            select new { Peo = g.Key, Count = g.Count() };
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}

  

转自 http://www.cnblogs.com/beginor/archive/2009/04/24/1442939.html
https://www.cnblogs.com/IT-Bear/archive/2013/07/25/3214512.html

https://www.cnblogs.com/IT-Bear/archive/2013/07/25/3214512.html

原文地址:https://www.cnblogs.com/venje/p/11573495.html