carrot1983 发表于 2015-9-18 13:58:04

用LinQ统计相同元素的数量

    class TextEntList
    {
      public string Ent { get; set; }
      public string Text { get; set; }
    }
    class Program
    {
      static void Main(string[] args)
      {
            ArrayList textents = new ArrayList();
            textents.Add(new TextEntList { Text = "L1", Ent = "e1", });
            textents.Add(new TextEntList { Text = "L11", Ent = "e2", });
            textents.Add(new TextEntList { Text = "L12", Ent = "e3", });
            textents.Add(new TextEntList { Text = "L1", Ent = "e4", });
            textents.Add(new TextEntList { Text = "L22", Ent = "e5", });
            textents.Add(new TextEntList { Text = "L12", Ent = "e6", });
            textents.Add(new TextEntList { Text = "L2", Ent = "e7", });

            var query =
                from TextEntList x in textents
                group x by x.Text into g
                select new { g.Key, N = g.Count() };

            foreach (var item in query)
            {
                Console.WriteLine(item);
            }

            Console.Write("Program finished, press Enter/Return to continue:");
            Console.ReadLine();
      }
    }



如果想要获得的结果:
{ Text = "L1" { Ent = "e1", Ent = "e4"}}。。。。 要怎么写,绕不弯来。。。




雪山飞狐_lzh 发表于 2015-9-30 09:36:47

本帖最后由 雪山飞狐_lzh 于 2015-9-30 10:08 编辑

分组的linq......是group by
获取的结果就是你要的

carrot1983 发表于 2015-9-30 10:32:46

雪山飞狐_lzh 发表于 2015-9-30 09:36 static/image/common/back.gif
分组的linq......是group by
获取的结果就是你要的

是的,知道是group by。
这玩意还不熟,后面只好用循环的笨办法收集了。
页: [1]
查看完整版本: 用LinQ统计相同元素的数量