聚合函数
Max 最大值
Min 最小值
Sum 求和
Average 求平均值
Aggregate 自定义累计
Count 统计元素个数
LongCount 统计元素个数返回long型
代码例子
1 List<int> tempList = new List<int> { 56, 34, 23, 21, 78, 99 }; 2 3 var maxValue = tempList.Max(); 4 var minValue = tempList.Min(); 5 var sumValue = tempList.Sum(); 6 var avgValue = tempList.Average(); 7 var aggValue = tempList.Aggregate((initial, next) => initial+next * 2); 8 var countValue = tempList.Count(); 9 var lcountValue = tempList.LongCount(); 10 Console.WriteLine("最大值:{0} 最小值:{1} 求和:{2} 平均值:{3} 自定义累计:{4} 元素个数:{5} 元素个数(long):{6}",maxValue,minValue,sumValue,avgValue,aggValue,countValue,lcountValue); 11 Console.ReadKey();
结果
Aggregate
该函数有三种定义,其中Func是Lambada表达式,seed是初始增量
var aggValue = tempList.Aggregate(6,(initial, next) => initial+next * 2);
初始是6,表示2倍的和再加上6,即628,该函数灵活多变。
join连接
内连接 inner join
内连接和SQL是类似的
查询语法:var p=from a in A join b in B on a.No equals b.No select new {};
方法语法:var p=A.Join(B,a=>a.No,b=>b.No,(a,b)=>new {});
左连接 left outer join
查询语法:var p=from a in A join b in B on a.No equals b.No into abLink from c in abLink DefaultIfEmpty()
select new {aa=a.No,bb=c==null?null:c.cc}
代码例子
定义Student类
1 public class Student 2 { 3 public string StudentNo { get; set; } 4 public string StudentName { get; set; } 5 public string ClassNo { get; set; } 6 }
定义ClassInfo类
1 public class ClassInfo 2 { 3 public string ClassNo { get; set; } 4 public string ClassName { get; set; } 5 }
实现调用
1 List<Student> listStudent = new List<Student> 2 { 3 new Student{StudentNo="1",StudentName="张三",ClassNo="1"}, 4 new Student{StudentNo="2",StudentName="李四",ClassNo="2"}, 5 new Student{StudentNo="3",StudentName="王五",ClassNo="3"}, 6 new Student{StudentNo="4",StudentName="刘三",ClassNo="1"}, 7 new Student{StudentNo="5",StudentName="关翊",ClassNo="2"}, 8 new Student{StudentNo="6",StudentName="张珊",ClassNo="3"}, 9 new Student{StudentNo="7",StudentName="赵四",ClassNo=""}, 10 }; 11 List<ClassInfo> listClassInfo = new List<ClassInfo> 12 { 13 new ClassInfo{ClassNo="1",ClassName="英才一班"}, 14 new ClassInfo{ClassNo="2",ClassName="英才二班"}, 15 new ClassInfo{ClassNo="3",ClassName="英才三班"}, 16 }; 17 18 var p = from s in listStudent 19 join c in listClassInfo 20 on s.ClassNo equals c.ClassNo 21 select new 22 { 23 studentname = s.StudentName, 24 classname=c.ClassName, 25 }; 26 var lp = listStudent.Join(listClassInfo, s =>s.ClassNo, c=>c.ClassNo,(s,c)=>new { studentname = s.StudentName,classname = c.ClassName }); 27 var linkp = from s in listStudent 28 join c in listClassInfo 29 on s.ClassNo equals c.ClassNo into scLink 30 from c in scLink.DefaultIfEmpty() 31 select new 32 { 33 studentname = s.StudentName, 34 classname = c?.ClassName 35 //classname = c==null?null:c.ClassName 36 }; 37 foreach (var item in linkp) 38 { 39 Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname); 40 } 41 Console.ReadKey();
输出结果
let
let用于设置临时变量储存结果,对数据进行操作
语法:let 变量a=操作。。。
1 var linkp = from s in listStudent 2 join c in listClassInfo 3 on s.ClassNo equals c.ClassNo into scLink 4 from c in scLink.DefaultIfEmpty() 5 let sc="中华一番"+c?.ClassName 6 select new 7 { 8 studentname = s.StudentName, 9 classname = sc 10 //classname = c==null?null:c.ClassName 11 }; 12 foreach (var item in linkp) 13 { 14 Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname); 15 } 16 Console.ReadKey()
上述代码在班级前加“中华一番”