标签:item intersect 注意 group div 返回 ons hashcode orderby
相关知识点复习:
1、var 类型推断: var p=new Person();
2、匿名类型: var a=new {Name="wang",Age=12 };
3、给新创建的对象属性赋值的简化方法: var P=new Person{Name="wang" ,Age=12 } 等价于 Person p=new Person() ; p.Name="wang"; p.Age=12;
4、.NET高级扩展方法:
var s0 = new Person { Name = "wang", Age = 12, Salary = 1200 }; var s1 = new Person { Name = "chen", Age = 13, Salary = 2400 }; var s2 = new Person { Name = "li", Age = 16, Salary = 2300 }; var s3 = new Person { Name = "qian", Age = 18, Salary = 1500 }; var s4 = new Person { Name = "liu", Age = 21, Salary = 2100 }; var s5 = new Person { Name = "zhao", Age = 23, Salary = 3211 }; var s6 = new Person { Name = "sun", Age = 27, Salary = 2390 }; List<Person> ps = new List<Person>(); ps.Add(s0); ps.Add(s2); ps.Add(s3); ps.Add(s6); var teacher1 = new Teacher() { Name = "wang",Students = {s0,s1,s2 } }; var teacher2 = new Teacher() { Name = "li" , Students = { s3, s4, s5 } }; Teacher[] ts = { teacher1, teacher2 };
(1)Any() ,判断集合是否包含元素,返回值是bool,一般比Count()>0效率高,Any还可以指定条件表达式。bool b=list.Any(p=>p.Age>50) 等价于bool b=list.Where(p=>=.Age>50).Any();
Console.WriteLine(ps.Any(p=>p.Salary>2000));
(2) Distinct(),剔除完全重复的数据。(*)注意自定义对象的Equal问题,需要重写Equal和GetHashCode方法来进行内容比较
(3)排序: 升序 list.OrderBy(p=>p.Age); 降序 list.OrderByDiscending(p=>p.Age) . 指定多个排序规则,而不是多个OrderBy,而是:list.OrderByDiscending(p=>p.Age).ThenBy(p=>p.Salary),也支持ThenByDiscending()
var p1 = ps.OrderBy(p => p.Salary); foreach (var p in p1) { Console.WriteLine("姓名:"+p.Name+"\n"+"薪水"+p.Salary); }
(4) skip(n)跳过前n条数据;Take(n)获取最多n条数据,如果不足也不会报错,常用来分页获取数据。 List.Skip(3).Take(2) 跳过前3条数据获取2条数据
var p1 = ps.Skip(2).Take(3); foreach (var p in p1) { Console.WriteLine("姓名:"+p.Name+"\n"+"薪水"+p.Salary); }
(5)Except(item1) 排除当前集合中在item1中存在的元素;
int[] num1 = { 3, 5, 8, 11, 15 }; int[] num2 = { 5, 8, 9, 15 }; var num3 = num1.Except(num2); foreach (var p in num3) { Console.WriteLine(p); }
(6)Union(item1) 把当前集合和item1中组合;
int[] num1 = { 3, 5, 8, 11, 15 }; int[] num2 = { 5, 8, 9, 15 }; var num3 = num1.Union(num2); foreach (var p in num3) { Console.WriteLine(p); }
(7)Intersect(item1)把当前集合和item1中取交集
int[] num1 = { 3, 5, 8, 11, 15 }; int[] num2 = { 5, 8, 9, 15 }; var num3 = num1.Intersect(num2); foreach (var p in num3) { Console.WriteLine(p); }
(8) 分组:
foreach (var p in ps.GroupBy(g1=>g1.Gender)) { Console.WriteLine(p.Key); foreach (var p2 in p) { Console.WriteLine(p2); } } Console.ReadKey();
(9)SelectMany: 把集合中的每个对象的另外集合属性的值重新拼接为一个新的集合;
foreach (var s in ts.SelectMany(t=>t.Students)) { Console.WriteLine(s); } Console.ReadKey();
标签:item intersect 注意 group div 返回 ons hashcode orderby
原文地址:https://www.cnblogs.com/fuyouchen/p/9378860.html