上一篇对LINQ的基础有了一个初步的认识,本博客主要就是为了实用,所以直接一起学习查询。
一、LINQ标准查询操作符
1、p投影操作符:将序列中的元素转换为一个由开发人员定义的形式的操作。
1)Select :对单个序列或集合中的值进行投影。
string[] Devices = { "电视", "电冰箱", "洗衣机", "电话", "微波炉" };
var SelectDevices = Devices.Select(device=>device);
foreach (string dev in SelectDevices1)
{
Console.WriteLine(“所有的设备:{0}", dev);
}
2)SelectMany:将序列的每个元素投影到 IEnumerable<(Of <(T>)>) 并将结果序列合并为一个序列。
class Devices { public string Name { get; set; } public List<double> Price { get; set; } } Devices[] DeviceGroup = { new Devices { Name = "电视", Price =new List<double>{ 3000,2000} }, new Devices { Name = "电冰箱", Price =new List<double> { 4000,5000 }}, new Devices { Name = "洗衣机", Price =new List<double> { 1500,1000 }} }; var devices = DeviceGroup.SelectMany(device => device.Price ); foreach (var v in devices) { Console.WriteLine(v); } }
2、限制操作符:按照一定的限制,对序列进行过滤。
1)Where:对单个序列或集合中的值进行投影。
string[] Devices = { "电视", "电冰箱", "洗衣机", "电话", "微波炉" }; var SelectDevices = Devices.Where(device=>device.StartsWith ("电")); foreach (string dev in SelectDevices) { Console.WriteLine("带电的设备:{0}", dev); }
3、排序操作符:按照升序和降序的方式对结果进行排序的功能。
1)OrderBy:将序列中的返回值按照升序的顺序进行排列。
string[] Devices = { "电视", "电冰箱", "洗衣机", "电话", "微波炉" }; var SelectDevices = Devices.OrderBy (device => device); foreach (string dev in SelectDevices) { Console.WriteLine(“所有的设备(排序后):{0}", dev); }
2)OrderByDescending:将序列中的返回值按照降序的顺序进行排列。
string[] Devices = { "电视", "电冰箱", "洗衣机", "电话", "微波炉" }; var SelectDevices = Devices.OrderByDescending(device => device); foreach (string dev in SelectDevices) { Console.WriteLine(“所有的设备(倒序后):{0}", dev); }
3) ThenBy:排序依据的次关键字。
Devices[] DeviceGroup = { new Devices { Name = "电视", Price = 10000 }, new Devices { Name = "电话", Price = 240 }, new Devices { Name = "电视", Price = 3000 } }; var devices = DeviceGroup.AsQueryable().OrderBy(device => device.Name).ThenBy(device => device.Price); foreach (var dev in devices) { Console.WriteLine("品名:{0},价格:{1}",dev.Name,dev.Price ); }
4)ThenByDescending:倒排序依据的次关键字。
Devices[] DeviceGroup = { new Devices { Name = "电视", Price = 10000 }, new Devices { Name = "电话", Price = 240 }, new Devices { Name = "电视", Price = 3000 } }; var devices = DeviceGroup.AsQueryable().OrderBy(device => device.Name).ThenByDescending(device => device.Price); foreach (var dev in devices) { Console.WriteLine("品名:{0},价格:{1}",dev.Name,dev.Price ); }
5)Reverse:反转序列。
string[] Devices = { "电视", "电冰箱", "洗衣机", "电话", "微波炉" }; var SelectDevices = Devices.Select(device=>device).Reverse (); foreach (string dev in SelectDevices) { Console.WriteLine("带电的设备:{0}", dev); }
4、联接操作符:将两个或多个数据源对象进行关联或联合。
1)Join:按照一定的条件,将两个数据源关联起来。
class StudentMessage//学生信息类 { public int ID//编号 { get; set; } public string Name//姓名 { get; set; } public bool Sex//姓别 { get; set; } public int Age//年龄 { get; set; } } class StudentScort//学生成绩类 { public int ID//编号 { get; set; } public float MathsScort//数学分数 { get; set; } public float ChineseScort//语文分数 { get; set; } }
这两个表是通过学生ID关联起来
接下来在程序入口中实例化出一些对象
static void Main() { List<StudentMessage> A_Class = new List<StudentMessage>(); //声名一个A班,来存放学员 A_Class.Add(new StudentMessage { ID = 1001, Name = "张三", Sex = true, Age = 20 }); //添加学员 A_Class.Add(new StudentMessage { ID = 1002, Name = "李四", Sex = false, Age = 21 });//添加学员 A_Class.Add(new StudentMessage { ID = 1003, Name = "王五", Sex = true, Age = 19 }); //添加学员 List<StudentScort> A_ScortSheet = new List<StudentScort>(); //声名一个成绩表 A_ScortSheet.Add(new StudentScort { ID = 1001, ChineseScort = 90f, MathsScort = 88f }); //添加学员成绩 A_ScortSheet.Add(new StudentScort { ID = 1002, ChineseScort = 80f, MathsScort = 68f }); //添加学员成绩 A_ScortSheet.Add(new StudentScort { ID = 1003, ChineseScort = 60f, MathsScort = 98f }); //添加学员成绩 var messages=A_Class.Join (A_ScortSheet ,mess=>mess. ID ,scos =>scos.ID ,(mes,sco)=>new {mes.ID, mes.Name, sco.ChineseScort, sco.MathsScort }); Console.WriteLine("{0,4} {1,10} {2,3} {3,3}", "学号","姓名","语文","数学"); foreach (var scort in messages) { Console.WriteLine("{0,6} {1,10} {2,4} {3,4}",scort .ID ,scort .Name ,scort.ChineseScort ,scort .MathsScort ); } }
2)GroupJoin:将主数据源中的每一个值或元素与次数据源中相应的值联接起来
interface Scort //成绩接口 { int ID //编号 { get; set; } float Scort //分数 { get; set; } string Subject //科目 { get; set; } } class StudentMessage//学生信息类 { public int ID//编号 { get; set; } public string Name//姓名 { get; set; } public bool Sex//姓别 { get; set; } public int Age//年龄 { get; set; } } class ChineaseScort : Scort //语文成绩类 { public int ID//编号 { get; set; } public float Scort { get; set; }//语文分数 public string Subject//科目 { get; set; } } class MathsScort : Scort//数学成绩类 { public int ID//编号 { get; set; } public float Scort { get; set; }//数学分数 public string Subject//科目 { get; set; } }
成绩接口ID和语文成绩和数学成绩ID关联,成绩接口和学生成绩类通过ID关联
static void Main() { List<StudentMessage> A_Class = new List<StudentMessage>(); //声名一个A班,来存放学员 A_Class.Add(new StudentMessage { ID = 1001, Name = "张三", Sex = true, Age = 20 }); //添加学员 A_Class.Add(new StudentMessage { ID = 1002, Name = "李四", Sex = false, Age = 21 });//添加学员 List<Scort> A_ScortSheet = new List<Scort>(); //声名一个成绩表 A_ScortSheet.Add(new ChineaseScort { ID = 1001, Scort = 100f, Subject = "语文" }); //添加语文成绩 A_ScortSheet.Add(new ChineaseScort { ID = 1002, Scort = 80f, Subject = "语文" }); //添加语文成绩 A_ScortSheet.Add(new MathsScort { ID = 1001, Scort = 90f, Subject = "数学" }); //添加数学成绩 A_ScortSheet.Add(new MathsScort { ID = 1002, Scort = 60f, Subject = "数学" }); //添加数学成绩 var messages = A_Class.GroupJoin(A_ScortSheet, mes => mes.ID, sco => sco.ID, (mess, scor) => new { Mess = mess.ID, Mename = mess.Name, Scor = scor.Select(Scor => Scor) }); foreach (var scort in messages) { Console.WriteLine("学号:{0,4} 姓名:{1}", scort.Mess, scort.Mename); foreach (var sc in scort.Scor) { Console.WriteLine(" {0}:{1}分", sc.Subject, sc.Scort); } }
5、分组操作符:按照一定的值将序列中的值或元素进行分组。
1)GroupBy:分组
class Student { public string ClassName { get; set; } public string Name { get; set; } } static void Main() { Student[] students = new Student[] { new Student{ClassName="A", Name="张三"}, new Student{ClassName ="B", Name ="李四"}, new Student{ClassName ="A" ,Name ="王五"}, new Student {ClassName ="B", Name ="赵六"}, new Student{ClassName ="B" ,Name ="钱七"}, new Student {ClassName ="B", Name ="孙八"} }; var stus = students.GroupBy(st => st.ClassName); foreach (var v in stus) { Console.WriteLine("{0}班学生:", v.Key); foreach (var va in v) { Console.WriteLine(" 姓名:{0}", va.Name); } } }
6、合并操作符:将两个对象合并在一起。
Concat:
string[] Citys = new string[] { "北京","上海","东京"}; string[] Mobiles = new string[] {"诺基亚","摩托罗拉","三星" }; var Cont = Citys.Select(city => city).Concat(Mobiles.Select(mobil => mobil)); foreach (var v in Cont) { Console.WriteLine(v); }
对于LINQ这些查询已基本满足平时操作的需求,对于其他的一些比如生成操作符,转换操作符等查询,当用到时在网上搜索一下,就可以很快理解!
好了,今天就到这里吧!