明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1400|回复: 2

[基础] 用LinQ统计相同元素的数量

[复制链接]
发表于 2015-9-18 13:58 | 显示全部楼层 |阅读模式
  1.     class TextEntList
  2.     {
  3.         public string Ent { get; set; }
  4.         public string Text { get; set; }
  5.     }
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             ArrayList textents = new ArrayList();
  11.             textents.Add(new TextEntList { Text = "L1", Ent = "e1", });
  12.             textents.Add(new TextEntList { Text = "L11", Ent = "e2", });
  13.             textents.Add(new TextEntList { Text = "L12", Ent = "e3", });
  14.             textents.Add(new TextEntList { Text = "L1", Ent = "e4", });
  15.             textents.Add(new TextEntList { Text = "L22", Ent = "e5", });
  16.             textents.Add(new TextEntList { Text = "L12", Ent = "e6", });
  17.             textents.Add(new TextEntList { Text = "L2", Ent = "e7", });

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

  22.             foreach (var item in query)
  23.             {
  24.                 Console.WriteLine(item);
  25.             }

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




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




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2015-9-30 09:36 来自手机 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2015-9-30 10:08 编辑

分组的linq......是group by
获取的结果就是你要的
 楼主| 发表于 2015-9-30 10:32 | 显示全部楼层
雪山飞狐_lzh 发表于 2015-9-30 09:36
分组的linq......是group by
获取的结果就是你要的

是的,知道是group by。
这玩意还不熟,后面只好用循环的笨办法收集了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-27 20:30 , Processed in 0.335252 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表