利用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM