//输出体重最大的同学,并要求最大体重得大于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. 使用miniui框架制作树形节点

    <div id="leftTree" class="mini-outlooktree" url="<%=basePath%>mana ...

  2. hdu 1872(看病要排队)(优先队列)

    看病要排队 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. ASP.NET应用中会话状态丢失及解决策略

    会话易丢失,解决办法 一. 了解下Web园 一个应用程序池默认是开启一个工作进程,但也可以开启多个工作进程,这样可提高性能,这个功能名为Web园,是小型的“Web农场”,您无需使用多台计算机来传送相同 ...

  4. webpack配置sass模块的加载

    webpack管理的项目,我们希望用sass定义样式,为了正常编译,需要做如下配置.这里不讲webpack的入门,入门的文章,我推荐这篇<webpack入门>. 为了使用sass,我们需要 ...

  5. Validate US Telephone Numbers

    function telephoneCheck(str) { // Good luck! //return true; var phone = /^1? ?(\d{3}|\(\d{3}\))[ -]? ...

  6. JavaWeb 学习004-增删改查的编写

    完成了grade,student模块的 数据库连接部分,还需要不断重复这个过程,熟练掌握JDBC的编写. 在不断编写的过程中,加深理解代码. 下一步 1.biz层面的知识内容 2.还有就是登陆成功后跳 ...

  7. spring缓存

    Spring Cache使用详解   复制过来时的地址:http://blog.csdn.net/xiaoyu411502/article/details/48901555 标签: spring-bo ...

  8. iphone中button按钮显示为圆形解决

    iphone中button按钮显示为圆形解决: 添加样式: -webkit-appearance:button; 如果需要为直角: border-radius:0 在源码中添加如:style=&quo ...

  9. 常见sizeof 笔试题

    最近面试过程中遇到了很多很多sizeof的问题. 现在总结一下: #include <stdio.h> #include <string.h> void fun(int arr ...

  10. Java getResourceAsStream() 方法会缓存文件的问题

    xxx.getClass().getClassLoader().getResourceAsStream("d:/test-config.properties") 这方法确实会缓存文 ...