Linq to Sql 聚合查询
//输出体重最大的同学,并要求最大体重得大于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 聚合查询的更多相关文章
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- (转)QueryBuilder : 打造优雅的Linq To SQL动态查询
原文地址:http://www.cnblogs.com/coolcode/archive/2009/09/28/IQueryBuilder.html 首先我们来看看日常比较典型的一种查询Form 这个 ...
- Linq to SQL 语法查询(子查询 & in操作 & join )
var 子查询 = from c in ctx.Customers where (from o in ctx.Ord ...
- sql 聚合查询
如果我们要统计一张表的数据量,例如,想查询students表一共有多少条记录,难道必须用SELECT * FROM students查出来然后再数一数有多少行吗? 这个方法当然可以,但是比较弱智.对于 ...
- LINQ to SQL Select查询
1. 查询所有字段 using (NorthwindEntities context = new NorthwindEntities()) { var order = from n in contex ...
- LINQ系列:LINQ to SQL Select查询
1. 查询全部字段 using (NorthwindContext context = new NorthwindContext()) { var expr = context.Products; f ...
- linq to sql 怎么查询前 11 条数据
(from 新表 in db.books where 新表.bookid < 400 select 新表).Take(11); storeDB.Albums.OrderByDescending( ...
- Linq to Sql:N层应用中的查询(上) : 返回自定义实体
原文:Linq to Sql:N层应用中的查询(上) : 返回自定义实体 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候,我们使 ...
- Linq to sql 的语法
Linq to SQL 语法查询(子查询 & in操作 & join ) 引用地址:http://www.cnblogs.com/82767136/articles/2949541.h ...
随机推荐
- 自动验证是ThinkPHP
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 数据验证有两种方式: 静态方式:在模型类里面通过$_validate属性定义验证规则 ...
- Android Studio插件整理
摘录于习惯沉默的Blog https://ydmmocoo.github.io/2016/06/28/Android-Studio%E6%8F%92%E4%BB%B6%E6%95%B4%E7%90% ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- C#多线程实现方法
C#中实现线程应用开发的类都包含在了System.Threading命名空间中,比较常用的是Thread,ThreadPool类 Thread类构造函数作用是创建线程,有两个重载版本,一个带参数,一个 ...
- 我总结的git命令指南。
git命令行指南 1.windows上安装git的网址:http://msysgit.github.com/. 没有安装过git的 这个网址里下mymsgit. 因为发现有些同学对git还不是那么熟悉 ...
- jqueyr获取动态创建的元素
javascript代码是按照代码顺序执行的,所以如果你用某个元素的click事件创建一个元素,id为test,然后在下面$("#test")是无法选择到的. 正确的方法是将在cl ...
- winxp计算机管理中服务详解
winxp计算机管理中服务详解01 http://blog.sina.com.cn/s/blog_60f923b50100efy9.html http://blog.sina.com.cn/s/blo ...
- 在 Linux 下将 PNG 和 JPG 批量互转的四种方法
计算机术语中,批处理指的是用一个非交互式的程序来执行一序列的任务[1]的方法.这篇教程里,我们会使用 Linux 命令行工具,并提供 4 种简单的处理方式来把一些 .PNG 格式的图像批量转换成 .J ...
- [Linux] 结构化命令 if
语法结构如下: 1. if-then语句 # if-then语句 if command #根据conmmand的退出状态码,选择执行语句 then commands fi e.g. #!usr/bin ...
- Iscroll解析
做了一些移动端的产品,发现一些滚动效果很多会使用 iscroll 作为底层库(如阿里小蜜).iscroll 的文档已经好久没更新了,而且比较简单,经常需要直接读源码.这里写一篇总结,作为对 iscro ...