Linq扩展函数浅谈
创始人
2024-06-06 22:01:15
0

XX有很多值得学习的地方,这里我们主要介绍Linq扩展函数,包括介绍Linq扩展函数Enumerable获取Counts等方面。

本文首先介绍Linq扩展函数(泛型函数)的方式提供的函数:
◆用不同方式产生结果集: Reverse<>(), ToArray<>(), ToList<>()
◆集合操作: Distinct<>(), Union<>(), Intersect<>()
◆统计函数: Count<>(), Sum<>(), Min<>(), Max<>()

Linq扩展函数Enumerable获取Counts

为了使用这些Enumerable扩展函数,一般把LINQ查询表达式用括号括起来,先转换为IEnumerable兼容的对象。

  1. static void GetCount()  
  2. {  
  3. string[] currentVideoGames = {"Morrowind", "BioShock",  
  4. "Half Life 2: Episode 1", "The Darkness",  
  5. "Daxter", "System Shock 2"};  
  6. // Get count from the query.  
  7. int numb = (from g in currentVideoGames  
  8. where g.Length > 6  
  9. orderby g  
  10. select g).Count();  
  11. // numb is the value 5.  
  12. Console.WriteLine("{0} items honor the LINQ query.", numb);  

定义演示的实例

  1. class Car  
  2. {  
  3. public string PetName = string.Empty;  
  4. public string Color = string.Empty;  
  5. public int Speed;  
  6. public string Make = string.Empty;  
  7. public override string ToString()  
  8. {  
  9. return string.Format("Make={0}, Color={1}, Speed={2}, PetName={3}",  
  10. Make, Color, Speed, PetName);  
  11. }  

 

  1. static void Main(string[] args)  
  2. {  
  3. Console.WriteLine("***** Fun with Query Expressions *****"n");  
  4. // This array will be the basis of our testing  
  5. Car[] myCars = new [] {  
  6. new Car{ PetName = "Henry"Color = "Silver"Speed = 100Make = "BMW"},  
  7. new Car{ PetName = "Daisy"Color = "Tan"Speed = 90Make = "BMW"},  
  8. new Car{ PetName = "Mary"Color = "Black"Speed = 55Make = "VW"},  
  9. new Car{ PetName = "Clunker"Color = "Rust"Speed = 5Make = "Yugo"},  
  10.  
  11. new Car{ PetName = "Hank"Color = "Tan"Speed = 0Make = "Ford"},  
  12. new Car{ PetName = "Sven"Color = "White"Speed = 90Make = "Ford"},  
  13. new Car{ PetName = "Mary"Color = "Black"Speed = 55Make = "VW"},  
  14. new Car{ PetName = "Zippy"Color = "Yellow"Speed = 55Make = "VW"},  
  15. new Car{ PetName = "Melvin"Color = "White"Speed = 43Make = "Ford"}  
  16. };  
  17. // We will call various methods here!  
  18. Console.ReadLine();  

【编辑推荐】

  1. Linq to SQL学习经验
  2. Linq隐式类型化局部变量
  3. Linq匿名类型简单概述
  4. Linq Lambda表达式剖析
  5. Linq对象初始值浅谈

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...