LinQ总结
不管是在Mvc还是在别的架构中的项目LinQ和Lambda总是经常会遇到的。
而有些LinQ的语法并不是很长用(我大部分用的是Lambda),所以有必要记录一下万一用到的时候我能很方便的找到我想找到的语法。
主要是对LinQ的from、where、select、orderby、group、join,简单的一个介绍
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LinQ各种语法 { class Program { static void Main(string[] args) { List<Student> list = new List<Student>() { new Student(){ StudentID = 1, Name = "张飞", Age = 38, Gender = 1}, new Student(){ StudentID = 2, Name = "关羽", Age = 48, Gender = 1}, new Student(){ StudentID = 3, Name = "刘备", Age = 37, Gender = 1}, new Student(){ StudentID = 4, Name = "貂蝉", Age = 20, Gender = 2}, new Student(){ StudentID = 5, Name = "孙尚香", Age = 19, Gender = 2}, new Student(){ StudentID = 6, Name = "甄宓", Age = 17, Gender = 2}, new Student(){ StudentID = 7, Name = "大乔", Age = 18, Gender = 2}, new Student(){ StudentID = 8, Name = "小乔", Age = 18, Gender = 2}, new Student(){ StudentID = 9, Name = "马云禄", Age = 20, Gender = 2}, new Student(){ StudentID = 10, Name = "蔡琰", Age = 21, Gender = 2}, new Student(){ StudentID = 11, Name = "祝融", Age = 17, Gender = 2}, new Student(){ StudentID = 12, Name = "关凤", Age = 16, Gender = 2}, new Student(){ StudentID = 13, Name = "隐藏", Age = 16, Gender = 2}, new Student(){ StudentID = 14, Name = "英雄", Age = 16, Gender = 2}, }; List<StudentExtension> list1 = new List<StudentExtension>() { new StudentExtension(){ ID = 1, StudentID = 1, Range = "蜀国", TeacherName = "张三"}, new StudentExtension(){ ID = 2, StudentID = 2, Range = "蜀国", TeacherName = "李四"}, new StudentExtension(){ ID = 3, StudentID = 3, Range = "蜀国", TeacherName = "王二"}, new StudentExtension(){ ID = 4, StudentID = 4, Range = "汉", TeacherName = "麻子"}, new StudentExtension(){ ID = 5, StudentID = 5, Range = "吴国", TeacherName = "王五"}, new StudentExtension(){ ID = 6, StudentID = 6, Range = "魏国", TeacherName = "赵六"}, new StudentExtension(){ ID = 7, StudentID = 7, Range = "吴国", TeacherName = "张三"}, new StudentExtension(){ ID = 8, StudentID = 8, Range = "吴国", TeacherName = "李四"}, new StudentExtension(){ ID = 9, StudentID = 9, Range = "蜀国", TeacherName = "王二"}, new StudentExtension(){ ID = 10, StudentID = 10, Range = "汉", TeacherName = "麻子"}, new StudentExtension(){ ID = 11, StudentID = 11, Range = "蜀国", TeacherName = "王五"}, new StudentExtension(){ ID = 12, StudentID = 12, Range = "蜀国", TeacherName = "赵六"}, new StudentExtension(){ ID = 12, StudentID = 12, Range = "蜀国", TeacherName = "赵六11"}, new StudentExtension(){ ID = 12, StudentID = 12, Range = "蜀国", TeacherName = "赵六22"}, new StudentExtension(){ ID = 12, StudentID = 12, Range = "蜀国", TeacherName = "赵六33"}, }; ///LinQ中的关键字 /// from 指定要查找的数据源及范围变量,多个from子句则表示从多个数据源中查找数据 /// select 自定查询要返回的目标数据,可以指定任何类型,甚至是匿名类型 /// where 指定元素的筛选条件,多个where子句则表示并列条件,必须全部满足才能入选 /// orderby 指定元素的排序字段和排序方式。当有多个排序字段时,有字段顺序确定主次关系,可以指定升序和降序两种排序方式 /// group 指定元素的分组字段 /// join 指定多个数据源的关联方式 ///from子句用来指定数据源 ///数据源是实现泛类型接口IEnumerable<T>或IQueryable<T>的类对象。 ///from localVar in dataSource ///dataSource 表示数据源 ///localVar 表示这个数据源中的其中一个的类型 ///select子句指定目标数据 ///指定在执行查询时产生结果的数据集中元素的类型 ///select element ///select 是关键字 ///element 参数指定查询结果中元素的类型及初始化方式 var linq = from student in list select new { student.Name, student.Age }; var l = linq.ToList(); foreach (var item in l) { Console.WriteLine(item); } foreach (var item in l) { Console.WriteLine(item.Name + "-" + item.Age); } Console.WriteLine("///select子句指定目标数据 返回一个匿名函数 end"); ///where子句指定筛选条件 ///对数据源中的数据进行过滤,筛选出满足条件的数据 ///where expression ///where 是关键字 ///expression 是一个逻辑表达式 var linq2 = from s in list where s.Age < 30 select s; foreach (var item in linq2) { Console.WriteLine(item.Name + "-" + item.Age + "-" + item.Gender + "-" + item.StudentID); } Console.WriteLine("where子句指定筛选条件 end"); ///orderby子句进行排序 ///通过orderby子句对查询结果进行排序操作,可以升序或者降序 ///orderby element [sortType] ///orderby 是关键字 ///element 是需要进行排序的子段 ///sortType 是可选字段,可以是ascending升序和descending降序两个选择,默认是升序 var linq3 = from s in list orderby s.Age select s; foreach (var item in linq3) { Console.WriteLine(item.Name + "-" + item.Age + "-" + item.Gender + "-" + item.StudentID); } Console.WriteLine("orderby子句进行排序 end"); ///group子句进行分组 ///group element by key ///group 是关键字 ///element 是分组的类型 和 from中德 element 是一个意思 ///by 是关键字 ///key 分组的字段 var linq1 = from s in list orderby s.Age descending group s by s.Age; foreach (var z in linq1) { Console.WriteLine(z.Key + "岁"); foreach (var s in z) { Console.WriteLine(" " + s.Name); } } ///join子句联接 ///join可以实现3种类型的联接分别为内部联接、分组联接和左外部联接 ///内部联接 /// join element in dataSource on exp1 equals exp2 /// 内联接 /// 使用这种方式,用来匹配的这个字段,两边都必须有一一对应的关键字 /// list和list1这两个列表中,两边必须一一对应上这条数据才会显示在结果集中 var linq4 = from s in list join c in list1 on s.StudentID equals c.StudentID select new { s.Name, s.StudentID, s.Age, s.Gender, c.TeacherName, c.Range }; foreach (var item in linq4) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "\t" + item.Range + "\t" + item.TeacherName + "\t"); } Console.WriteLine("join子句联接---内部联接 end"); ///分组联接 /// join element in dataSource on exp1 equals exp2 into grpName /// 这种方法首先以list为基础联接list1,并且对list1匹配到的数据进行重命名 into grpName /// (注意这个时候grpName是一个集合,它里面的数据就是和list匹配到的list1的数据,可能匹配到0条或者多条,他的类型就是list1的类型) /// select new { s.Name, s.StudentID, s.Age, s.Gender, v = grpName}; /// 所以select的时候就要这样写 /// v = 一个 list1 的集合 /// 是用这种方式是一第一种数据源(list)为基础,在那匹配到的第二个数据源(list1)往第一个数据源上拼数据 var linq5 = from s in list join c in list1 on s.StudentID equals c.StudentID into grpName select new { s.Name, s.StudentID, s.Age, s.Gender, v = grpName}; foreach (var item in linq5) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "我是Key"); foreach (var item1 in item.v)//item.v中是匹配到的数据集合 { Console.WriteLine(item1.ID + "\t" + item1.Range + "\t" + item1.StudentID + "\t" + item1.TeacherName); } } Console.WriteLine("join子句联接---分组联接 end"); Console.WriteLine("------------------------------"); ///左外部联接 /// join element in dataSource on exp1 equals exp2 into grpName /// from grp in grpName.DefaultIfEmpty() /// 注意如果 grp 匹配到的数据为null了,直接报异常
/// 所以 使用grp里面的数据的时候需要判读grp是否为null,所以这里加一个三元表达式就行了
/// grp == null ? "" : grp.Range
var linq6 = from s in list join c in list1 on s.StudentID equals c.StudentID into grpName from grp in grpName.DefaultIfEmpty() select new { s.Name, s.StudentID, s.Age, s.Gender, grp.TeacherName, grp.Range }; foreach (var item in linq6) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "\t" + item.Range + "\t" + item.TeacherName + "\t"); } Console.WriteLine("join子句联接---左外部联接 end"); Console.ReadLine(); } } public class Student { public int StudentID { get; set; } public string Name { get; set; } public int Age { get; set; } /// <summary> /// 1男2女3未设置 /// </summary> public int Gender { get; set; } } public class StudentExtension { public int ID { get; set; } public int StudentID { get; set; } public string Range { get; set; } public string TeacherName { get; set; } } }
Linq 中的模糊查询
//StartsWith("孙") 相当于孙% var linq7 = (from s in list where s.Name.StartsWith("孙") select s).ToList(); //IndexOf("尚") != -1 相当于%尚% var linq8 = (from s in list where s.Name.IndexOf("尚") != -1 select s).ToList(); //EndsWith("香") 相当于%香 var linq9 = (from s in list where s.Name.EndsWith("香") select s).ToList();