zoukankan      html  css  js  c++  java
  • Linq.GroupBy使用示例

        public class TestClass
        {
            public static void TestIt()
            {
                var ds = DateTime.Now;
                var list = new List<MA>();
                list.Add(new MA { ma001 = "A", ma002 = "1" });
                list.Add(new MA { ma001 = "A", ma002 = "2" });
                list.Add(new MA { ma001 = "A", ma002 = "F" });
                list.Add(new MA { ma001 = "B", ma002 = "1" });
                list.Add(new MA { ma001 = "B", ma002 = "D" });
                list.Add(new MA { ma001 = "C", ma002 = "E" });
                list.Add(new MA { ma001 = "A", ma002 = "3" });
                list.Add(new MA { ma001 = "B", ma002 = "A" });
                var listG = list.GroupBy(p => p.ma001);
                foreach (var g in listG)
                {
                    var keys = list.Where(p => p.ma001 == g.Key).Aggregate("", (current, p) => current + p.ma002);
                    foreach (var p in list.Where(p => p.ma001 == g.Key))
                    {
                        p.ma003 = keys;
                    }
                }
                foreach (var p in list)
                {
                    Console.WriteLine(string.Format("{0},{1},{2}", p.ma001, p.ma002, p.ma003));
                }
                var de = DateTime.Now;
                Console.WriteLine("计算耗时(ms):" + (de - ds).TotalMilliseconds);
            }
        }
        public class MA
        {
            public string ma001 { get; set; }
            public string ma002 { get; set; }
            public string ma003 { get; set; }
        }
    

      

  • 相关阅读:
    第六次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业 计科一班程晨
    第一次作业 计科一班程晨
    第七次作业
    第五次作业
    第四次作业
    第三次作业
  • 原文地址:https://www.cnblogs.com/blackice/p/2851115.html
Copyright © 2011-2022 走看看