LInq 与lambda表达式
LinQ是我们常用的技术之一。因为我们绕不开的要对数据进行一系列的调整,如 排序、 条件筛选、求和、分组、多表联接 等等。
lambda则是我们常用的语法糖,配合linq使用天衣无缝,不知不觉就用上了,主要还是看着代码简洁,逼格够高。。。
接下来我们就来看看常用的有哪些
先来点基础数据
//用户类 public class User { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Address { get; set; } [DataMember] public string Phone { get; set; } [DataMember] public string Sex { get; set; } [DataMember] public int Age { get; set; } [DataMember] public int SchID { get; set; } }
/// <summary> /// 学校类 /// </summary> internal class School { public int SchID { get; set; } public string SchName { get; set; } public School(){} public School(int id,string name) { SchID = id; SchName = name; } } /// <summary> /// 公司类 /// </summary> internal class Company { public string Name { get; set; } public List<User> Users { get; set; } public Company() { } public Company(string name, List<User> list) { Name = name; Users = list; } }
接下来初始化点测试数据
User user1 = new User() { ID = 111, Name = "张一", Address = "上海市某某区", Phone = "13547878787", Age = 30, Sex = "男", SchID=1 }; User user2 = new User() { ID = 112, Name = "李小二", Address = "上海市某某区", Phone = "13547878783", Age = 30, Sex = "女", SchID = 1 }; User user3 = new User() { ID = 113, Name = "张三", Address = "上海市某某区", Phone = "13547878784", Age = 30, Sex = "男", SchID = 1 }; User user4 = new User() { ID = 114, Name = "李四", Address = "上海市某某区", Phone = "13547878785", Age = 30, Sex = "男", SchID = 1 }; List<User> userlist = new List<User>(4) { user1, user2, user3, user4 };
List<School> Schlist = new List<School>() {
new School(1,"武汉大学"),new School(2,"华中科技大学"),new School(3,"华中师范大学")
};
统计(求和)Sum
//求和 var sum = userlist.Where(a => { return a.ID > 0; }).Sum(a=>a.ID); Console.WriteLine(sum);
//另一种写法 sum =(from a in userlist where a.ID>0 select a.ID ).Sum(); Console.WriteLine(sum);
IEnumerable.Where()方法里面的参数数委托类型Func<TSource, bool> 前面为参数 最后面一个为返回值
求最大最小值 Max Min
//求最大值 var max = userlist.Max(a => a.ID); Console.WriteLine(max); //最小值 var min = userlist.Min(a => a.ID); Console.WriteLine(min);
循环 ForEach
//循环输出 userlist.ForEach(a => { if (a.Age > 20) { Console.WriteLine(a.ID); } });
条件筛选 where
//筛选 var user = userlist.Where(a => a.ID == 114).Single(); //筛选所有男性用户 var templist = userlist.Where(a => a.Sex == "男").ToList();
排序
//排序 根据ID逆序 templist = userlist.OrderByDescending(a => a.ID).ToList(); //升序 templist = userlist.OrderBy(a => a.ID).ToList();
分组 ToLookup 和 GroupBy
//分组 var lookup = userlist.ToLookup(a => a.Sex); foreach (var item in lookup) { Console.WriteLine(item.Key); foreach (var sub in item) { Console.WriteLine("\t\t"+sub.Name+" "+sub.Age); } } //另一种 var dic = userlist.GroupBy(a => a.Sex); foreach (var item in dic) { Console.WriteLine(item.Key); foreach (var sub in item) { Console.WriteLine("\t\t" + sub.Name + " " + sub.Age); } }
联接 (这里只展示内联接)Join
//联接 var temp = from usertemp in userlist join sch in Schlist on usertemp.SchID equals sch.SchID select new { Id = usertemp.ID, Name = usertemp.Name, Age = usertemp.Age,Schname=sch.SchName };
返回的匿名类,也可以指点实体类
类型查找 OfType
类型查找很少用到 因为我们都是用的泛型,不过这里还是列出来看看
//类型查找 List<object>objlist =new List<object>(){1,"2",false,"s",new User(){ID=1,Name="xx"}}; IEnumerable<string> query1 = objlist.OfType<string>(); foreach (string fruit in query1) { Console.WriteLine(fruit); }
可以看到把2个字符串类型给查找出来了
查找深层嵌套 SelectMany
selectMany很少人用也比较少人知道,它可以避免多层嵌套循环 其使用场景还是挺多的,见下代码
//查找深层嵌套 //初始化数据 Company ChinaMobile = new Company("中国移动",userlist); Company ChinaUnicom = new Company("中国联通", userlist); List<Company> companylist = new List<Company>() { ChinaMobile, ChinaUnicom }; //找出2个公司所有女性成员 var selectlist = companylist.SelectMany(a => a.Users).Where(b => b.Sex == "女"); foreach (var item in selectlist) { Console.WriteLine(item.Name+":"+item.Sex); }
这里可以看到 给出的数据源都是同一个list集合 然后我们看每个集合中只有一个女性成员
正常情况下要想取得这数据的话 要经过2层嵌套循环
foreach (Company c in companylist) { foreach (var item in c.Users) { if (item.Sex == "女") { Console.WriteLine(item.Name + ":" + item.Sex); } } }
分类: C#知识体系
标签: Linq lambda