Group BY 分组数据,查找数据缩小范围
Group By 分组查询数据
Var GroupBy = from StuTT in db.Student // 将对象提取出来 group StuTT by stuTT.StuID into g // 表示对StuTT按StuID字段归类,其命名结果为 g,StuTT作用域结束 select g;
不重写命名也可以,
Var GroupBy = from StuTT in db.Student group StuTT by stuTT.StuID ;
如果要遍历查询出的结果集 例;
Foreach(var item in GroupBy) { if(item .KEY > 10) //这里加入判断 { froeach(var p in item) { } } }
Select 匿名类别
对返回结果集进行重新包装
var SelectTt = from S in db.Student group S by S.StuID into g select new {StuID = g.Key,g }
遍历匿名类中所有记录同上述一样,
最大值
var max = from S in Student group S by S.Stuid into g select new {g.KEY,StuAge = g.MAX(g=>g.age) }
min(最小),Average(平均),Sum(求和),Count(计数),与求最大值一样,掠过,注意 条件类型
ORDER BY 排序,默认为升序,DESCENDING 降序
简单形式
var OrderBY = from S in Student order by S.StuID Select S
VAR Orderby = from S in Student Orderby s.StuID descending select S;//降序排列
使用复合Order by 排序
var OrderBY = from S in Student Orderby S.StuID,S.SutAge //按StuID排序后再按StuAge排序 select S;
用 Lambda 表达式 写上述效果
var Lambda = db.Student .orderby(S=>s.StuID) .ThenBy(S=>s.StuAge).tolist();
摘抄,原文地址(http://kb.cnblogs.com/page/42469/2/)