C#基础:LINQ查询语法和⽅法语法
下⾯使⽤LINQ创建⼀个查询数组数据的⽰例:
查询语法:
1. //names数组
2. string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh",
"Samba", "Fatimah" };
3. //linq的查询语法
4. var queryResults = //⽤var声明查询结果变量
5. from n in names //from 指定要插叙的数据
6. where n.StartsWith("S") //where 指定条件,以"S"为起点
7. orderby n //排序
8. select n; //选择元素
9. Console.WriteLine("Names beginning with S:");writeline方法的作用
10. foreach (var item in queryResults) //foreach循环输出
11. {
12. Console.WriteLine(item);
13. }
⽅法语法:Lambda表达式
1. //names数组
2. string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Rodriguez", "Hsieh", "Jorgenson",
"Ilyich", "Singh", "Samba", "Fatimah" };
3. //⽅法语法,使⽤Lambda表达式
4. var queryResults = names.Where(n => n.StartsWith("S"));
5. Console.WriteLine("Names beginning with S:");
6. foreach (var item in queryResults)//foreach循环输出查询结果
7. {
8. Console.WriteLine(item);
9. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论