Lambda中的一些方法的总结
public List<UserInfoBaseModel> GetNameByIDList(List<int> UserID) { var UserList = LoadRepository<User_Info>() .GetModel() .Where(x => UserID.Contains(x.UserInfoID)) .Select(i => new UserInfoBaseModel() { UserInfoId = i.UserInfoID, UserName = i.UserName }) .ToList(); return UserList; }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lambda的各种语法 { 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"}, }; ///select 返回一个匿名函数 var lambda1 = list.Select(i => new { i.Age, i.Name, i.StudentID, i.Gender }); foreach (var item in lambda1) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender); } Console.WriteLine("select -----end"); ///where var lambda2 = list.Where(i => i.Age < 30).Select(i => new { i.Age, i.Name, i.StudentID, i.Gender }); foreach (var item in lambda2) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender); } Console.WriteLine("where -----end"); ///OrderBy 升序 var lambda3 = list.OrderBy(i => i.Age); foreach (var item in lambda3) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender); } Console.WriteLine("OrderBy -----end"); ///OrderByDescending 降序 var lambda4 = list.OrderByDescending(i => i.Age); foreach (var item in lambda4) { Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender); } Console.WriteLine("OrderByDescending -----end"); //group子句进行分组 var lambda5 = list.GroupBy(i => i.Age); foreach (var item in lambda5) { Console.WriteLine(item.Key); foreach (var item1 in item) { Console.WriteLine(item1.StudentID + "\t" + item1.Name + "\t" + item1.Age + "\t" + item1.Gender); } } ///多表链接 ///lambda内连接 var lambda6 = list.Join(list1, x => x.StudentID,//x对应的是list c => c.StudentID,//c对应的是list1 (x, c) => new StudentView()//第三个参数对设置返回的数据类型 { Age = x.Age, Range = c.Range, Name = x.Name, Gender = x.Gender, StudentID = x.StudentID, TeacherName = c.TeacherName }).ToList(); ///lambda分组链接 var lambda7 = list.GroupJoin(list1, x => x.StudentID,//x对应的是list c => c.StudentID,//c对应的是list1 (x, c) => new //第三个参数对设置返回的数据类型 { Age = x.Age, Name = x.Name, Gender = x.Gender, StudentID = x.StudentID, V = c }).ToList(); foreach (var item in lambda7) { 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.ReadLine(); } } public class Student { [DisplayName("学生的ID")] public int StudentID { get; set; } [DisplayName("学生姓名")] public string Name { get; set; } [DisplayName("学生的年龄")] public int Age { get; set; } /// <summary> /// 1男2女3未设置 /// </summary> [DisplayName("学生的性别")] 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; } } public class StudentView { 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 int ID { get; set; } public string Range { get; set; } public string TeacherName { get; set; } } }