//输出体重最大的同学,并要求最大体重得大于39,并按照体重大下,对分组结果进行排序。
var result = from query in linq.Student
group query by query.ClassID into gS
where gS.Max<Student>(s => s.StudentWeigth) > 39
orderby gS.Max<Student>(s => s.StudentWeigth) descending
select new
{
ClassID = gS.Key,
MaxWeight = gS.Max<Student>(s => s.StudentWeigth)
}; var result = from query in linq.Student
group query by query.ClassID into gS let mw = gS.Max<Student>(s => s.StudentWeigth) where mw > 39
select new
{
ClassID = gS.Key,
MaxWeight = mw
};
foreach (var item in result)
{
Response.Write(string.Format("classid = {0} studentmaxweight = {1}", item.ClassID, item.MaxWeight));
}
//查询身高大于132并且体重大于30的Student,并按照StudentID升序排序,按照classID降序排序
var query = from s in db.Students
where s.HeightInCm > 132 && s.WeightInKg > 30
orderby s.StudentID ascending, s.ClassID descending
select s;
//对Student表按照ClassID和Hometown两个字段进行分组,并输出每个班级中某个地方的学生数
var query = from s in db.Students
group s by new { s.ClassID, s.Hometown } into gS
let cn = gS.Count<Student>()
select new
{
ClassID = gS.Key.ClassID,
Hometown = gS.Key.Hometown,
Count = cn
};
foreach (var item in query)
{
Console.WriteLine("class id = {0} hometown {1} student count = {2}", item.ClassID, item.Hometown,item.Count);
}
//在上面的基础上加一点点需求,要求分组后的结果按照count排序
var query = from s in db.Students
group s by new { s.ClassID, s.Hometown } into gS
let cn = gS.Count<Student>()
orderby cn descending
select new
{
ClassID = gS.Key.ClassID,
Hometown = gS.Key.Hometown,
Count = cn
};

Linq to Sql 聚合查询的更多相关文章

  1. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  2. (转)QueryBuilder : 打造优雅的Linq To SQL动态查询

    原文地址:http://www.cnblogs.com/coolcode/archive/2009/09/28/IQueryBuilder.html 首先我们来看看日常比较典型的一种查询Form 这个 ...

  3. Linq to SQL 语法查询(子查询 & in操作 & join )

    var 子查询 = from c in ctx.Customers                    where                        (from o in ctx.Ord ...

  4. sql 聚合查询

    如果我们要统计一张表的数据量,例如,想查询students表一共有多少条记录,难道必须用SELECT * FROM students查出来然后再数一数有多少行吗? 这个方法当然可以,但是比较弱智.对于 ...

  5. LINQ to SQL Select查询

    1. 查询所有字段 using (NorthwindEntities context = new NorthwindEntities()) { var order = from n in contex ...

  6. LINQ系列:LINQ to SQL Select查询

    1. 查询全部字段 using (NorthwindContext context = new NorthwindContext()) { var expr = context.Products; f ...

  7. linq to sql 怎么查询前 11 条数据

    (from 新表 in db.books where 新表.bookid < 400 select 新表).Take(11); storeDB.Albums.OrderByDescending( ...

  8. Linq to Sql:N层应用中的查询(上) : 返回自定义实体

    原文:Linq to Sql:N层应用中的查询(上) : 返回自定义实体 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候,我们使 ...

  9. Linq to sql 的语法

    Linq to SQL 语法查询(子查询 & in操作 & join ) 引用地址:http://www.cnblogs.com/82767136/articles/2949541.h ...

随机推荐

  1. php js 排序

     编写背景及排序 规则 公司需要对游戏进行一系列的排序,在这里只说我自己遇到问题的哪一段 //规则:$plat数据要根据$sort里的sort为相应 可以输入一个数字,即为该平台: 解决思路:将$so ...

  2. codeforces Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)// 二分的题目硬生生想出来ON的算法

    A. Road to Cinema 很明显满足二分性质的题目. 题意:某人在起点处,到终点的距离为s. 汽车租赁公司提供n中车型,每种车型有属性ci(租车费用),vi(油箱容量). 车子有两种前进方式 ...

  3. Brocade博科光纤交换机zone配置

    1.规划 交换机 端口 用途 DS6520B-A 94 存储模块1-1 95 存储模块2-1 68 DB1网卡1-1 69 DB2网卡1-1 DS6520B-B 94 存储模块1-2 95 存储模块2 ...

  4. 深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow

    深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow 最近在公司做深度学习相关的学习和实验,原来一直 ...

  5. 問題排查:类型“System.DateTime”的对象无法转换为类型“System.String”

    最近在擴充資料對接工具的功能 經常會遇到這個狀況 當然還有其他同類提示,例如 int/decimal 無法轉 System.String 等等 無獨有偶 這些錯誤幾乎都是在 DataTable 轉換成 ...

  6. XE3随笔16:将字符串转换成 UTF8 编码的函数

    这种转换一般用于网页地址; 我不知道 Delphi 是不是有现成的函数, 用到了就写了一个. //函数: function ToUTF8Encode(str: string): string; var ...

  7. MCMC and Bayesian Data Analysis(PPT在文件模块)

    How to generate a sample from $p(x)$? Let's first see how Matlab samples from a $p(x)$. In Matlab, t ...

  8. 图表插件使用汇总(echarts,highchairts)

    1.echarts之饼图显示数字 option={ title: { text: '某站点用户访问来源', subtext: '纯属虚构', x: 'center' }, tooltip: { tri ...

  9. STL的迭代器和类型萃取

    今天就可以把STL库中迭代器的实现,和类型萃取好好整理一下了 迭代器的设计思维是STL的关键所在,在STL的实际运用和泛型思维,迭代器都扮演着十分重要的角色,STL力求把数据容器和算法的概念分开来,于 ...

  10. QQ在线客服JS代码,自适应漂浮在网页右侧

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...