C# linq语句学习
using System; using System.Linq; namespace ConsoleApp1 { class Program { static void Main(string[] args) { int[] arr = { 1, 2, 3, 4, 5, 6 }; var a = from num in arr where num % 2 == 0 select num; foreach (var i in a) { Console.WriteLine(i); } Console.ReadKey(); var b = arr.Where(x => x % 2 == 0).OrderBy(n=>n); foreach (var i in b) { Console.WriteLine(i); } Console.ReadKey(); } } }
使用LINQ查询.NET对象
using System; using System.Data; using System.Collections.Generic; using System.Linq; namespace ConsoleApp2 { class Program { static void Main(string[] args) { List<Student> list = new List<Student>() { new Student() { Name="张三丰", sex="男", Age=108 }, new Student() { Name="李三丰", sex="男", Age=108 }, new Student() { Name="李三丰", sex="女", Age=108 } }; var query = from item in list where item.Name.StartsWith("张") && item.sex == "男" select item; foreach(var item in query) { Console.WriteLine(item.Name); } Console.ReadKey(); } } }
查询语句用用查询方法代替:
var query = list.Where(item => item.Name.StartsWith("张") && item.sex == "男");
查询类型是ArrayList的是需要在变量前增加类型的定义:
using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.Linq; namespace ConsoleApp2 { class Program { static void Main(string[] args) { ArrayList list = new ArrayList() { new Student() { Name="张三丰", sex="男", Age=108 }, new Student() { Name="李三丰", sex="男", Age=108 }, new Student() { Name="李三丰", sex="女", Age=108 } }; var query = from Student item in list where item.Name.StartsWith("张") && item.sex == "男" select item; foreach (Student item in query) { Console.WriteLine(item.Name); } Console.ReadKey(); } } }
用索引器筛选:
List<Student> list = new List<Student>(){ new Student(){ Name="张三丰", Sex="男", Age=21 }, new Student(){ Name="张三丰", Sex="男", Age=21 }, new Student(){ Name="李三丰", Sex="男", Age=21 }, new Student(){ Name="李三丰", Sex="男", Age=21 }, new Student(){ Name="李四", Sex="女", Age=21 }, }; var query = list.Where(( item,index )=>item.Name.StartsWith("李")&&item.Sex=="女"&&index%2==0); foreach(Student stu in query){ Console.WriteLine(stu.Name); }
类型筛选:
object[] arr = {"str",1,3,"mlh"}; var query = arr.OfType<string>(); foreach(var item in query){ Console.WriteLine(item); }
复合的from子句
List<Student> list = new List<Student>(){ new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,23} }, new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,32 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,35 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,36 } }, new Student(){ Name="李四", Sex="女", Age=new int[]{ 21,3} }, }; var query =from item in list from age in item.Age where age==3 select item; foreach(Student stu in query){ Console.WriteLine(stu.Name); }
select子句:
List<Student> list = new List<Student>(){ new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,23} }, new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,32 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,35 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,36 } }, new Student(){ Name="李四", Sex="女", Age=new int[]{ 21,3} }, }; var query =from item in list from age in item.Age where age==3 select item.Name; foreach(string item in query){ Console.WriteLine(item); }
查询方法:
List<Student> list = new List<Student>(){ new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,23} }, new Student(){ Name="张三丰", Sex="男", Age=new int[]{ 21,32 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,35 } }, new Student(){ Name="李三丰", Sex="男", Age=new int[]{ 21,36 } }, new Student(){ Name="李四", Sex="女", Age=new int[]{ 21,3} }, }; var query = list.Where((item) => { return item.Name.StartsWith("李"); }).Select(item=>item.Name); foreach(string item in query){ Console.WriteLine(item); }
group by 分组:
List<Student> list = new List<Student>(){ new Student(){Name="****",Sex="男",Age=new int[]{20,30}}, new Student(){Name="***",Sex="女",Age=new int[]{20,30}}, new Student(){Name="***",Sex="女",Age=new int[]{20,30}}, new Student(){Name="石盐",Sex="男",Age=new int[]{10,11}}, new Student(){Name="梅盐",Sex="男",Age=new int[]{10,11}}, }; var query = from item in list group item by item.Sex;foreach(var stu in query){ foreach(var item in stu){ Console.WriteLine(item.Name); } }