聚合函数
Max 最大值
Min 最小值
Sum 求和
Average 求平均值
Aggregate 自定义累计
Count 统计元素个数
LongCount 统计元素个数返回long型
代码例子
List<int> tempList = new List<int> { 56,34,23,21,78,99 };
var maxValue = tempList.Max();
var minValue = tempList.Min();
var sumValue = tempList.Sum();
var avgValue = tempList.Average();
var aggValue = tempList.Aggregate((initial,next) => initial+next * 2);
var countValue = tempList.Count();
var lcountValue = tempList.LongCount();
Console.WriteLine("最大值:{0}\r\n最小值:{1}\r\n求和:{2}\r\n平均值:{3}\r\n自定义累计:{4}\r\n元素个数:{5}\r\n元素个数(long):{6}",maxValue,minValue,sumValue,avgValue,aggValue,countValue,lcountValue);
Console.ReadKey();
结果
Aggregate
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
该函数有三种定义,其中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类
public class Student
{
public string StudentNo { get; set; }
public string StudentName { get; set; }
public string ClassNo { get; set; }
}
定义ClassInfo类
public class ClassInfo
{
public string ClassNo { get; set; }
public string ClassName { get; set; }
}
实现调用
List<Student> listStudent = new List<Student>
{
new Student{StudentNo="1",StudentName="张三",ClassNo="1"},
new Student{StudentNo="2",StudentName="李四",ClassNo="2"},
new Student{StudentNo="3",StudentName="王五",ClassNo="3"},
new Student{StudentNo="4",StudentName="刘三",ClassNo="1"},
new Student{StudentNo="5",StudentName="关翊",ClassNo="2"},
new Student{StudentNo="6",StudentName="张珊",ClassNo="3"},
new Student{StudentNo="7",StudentName="赵四",ClassNo=""},
};
List<ClassInfo> listClassInfo = new List<ClassInfo>
{
new ClassInfo{ClassNo="1",ClassName="英才一班"},
new ClassInfo{ClassNo="2",ClassName="英才二班"},
new ClassInfo{ClassNo="3",ClassName="英才三班"},
};
var p = from s in listStudent
join c in listClasInfo
on s.ClassNo equals c.ClassNo
select new
{
studentname = s.StudentName,
classname = c.ClassName,
};
var lp = listStudent.Join(listClassInfo,s=>s.ClassNo,c=>c.ClassNo,(s,c)=>new { studentname = s.StudentName,classname = c.ClassName}
var linkp = from s in listStudent
join c in listClassInfo
on s.ClassNo equals c.ClassNo into scLink
from c in scLink.DeafaultIfEmpty()
slect new
{
studentname = s.StudentName,
classname = c?.ClassName
//className = c==null?null:c.ClassName
};
foreach (var item in lp)
{
Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname),
}
Console.ReadKey();
输出结果
let
let用于设置临时变量储存结果,对数据进行操作
语法:let 变量a=操作。。。
var linkp = from s in listStudent
join c in listClassInfo
on s.ClassNo equals c.ClassNo into scLink
from s in scLink.DefaultIfEmpty()
let sc="中华一番"+c?.ClassName
select new
{
studentname = s.StudentName,
classname = sc
};
foreach (var item in linp)
{
Console.WriteLine("姓名:{0},班级:{1}",item.studentname,item.classname);
}
Console.ReadKey();
上述代码在班级前加“中华一番”