您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了“Linq扩展函数有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linq扩展函数有哪些”这篇文章吧。
首先介绍Linq扩展函数(泛型函数)的方式提供的函数:
◆用不同方式产生结果集: Reverse<>(), ToArray<>(), ToList<>()
◆集合操作: Distinct<>(), Union<>(), Intersect<>()
◆统计函数: Count<>(), Sum<>(), Min<>(), Max<>()
Linq扩展函数Enumerable获取Counts
为了使用这些Enumerable扩展函数,一般把LINQ查询表达式用括号括起来,先转换为IEnumerable<T>兼容的对象。
static void GetCount() { string[] currentVideoGames = {"Morrowind", "BioShock", "Half Life 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}; // Get count from the query. int numb = (from g in currentVideoGames where g.Length > 6 orderby g select g).Count<string>(); // numb is the value 5. Console.WriteLine("{0} items honor the LINQ query.", numb); }
定义演示的实例
class Car { public string PetName = string.Empty; public string Color = string.Empty; public int Speed; public string Make = string.Empty; public override string ToString() { return string.Format("Make={0}, Color={1}, Speed={2}, PetName={3}", Make, Color, Speed, PetName); } }
static void Main(string[] args) { Console.WriteLine("***** Fun with Query Expressions *****"n"); // This array will be the basis of our testing Car[] myCars = new [] { new Car{ PetName = "Henry", Color = "Silver", Speed = 100, Make = "BMW"}, new Car{ PetName = "Daisy", Color = "Tan", Speed = 90, Make = "BMW"}, new Car{ PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"}, new Car{ PetName = "Clunker", Color = "Rust", Speed = 5, Make = "Yugo"}, new Car{ PetName = "Hank", Color = "Tan", Speed = 0, Make = "Ford"}, new Car{ PetName = "Sven", Color = "White", Speed = 90, Make = "Ford"}, new Car{ PetName = "Mary", Color = "Black", Speed = 55, Make = "VW"}, new Car{ PetName = "Zippy", Color = "Yellow", Speed = 55, Make = "VW"}, new Car{ PetName = "Melvin", Color = "White", Speed = 43, Make = "Ford"} }; // We will call various methods here! Console.ReadLine(); }
以上是“Linq扩展函数有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。