//输出体重最大的同学,并要求最大体重得大于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. SQL Server 通用分页存储过程

    create proc commonPagination ), --要显示的列名,用逗号隔开 ), --要查询的表名 ), --排序的列名 ), --排序的方式,升序为asc,降序为 desc ), ...

  2. C/C++ 记录时间

    http://stackoverflow.com/questions/2808398/easily-measure-elapsed-time https://github.com/picanumber ...

  3. git 常用操作

    查看某文件的某些行的变化历史: $ git log --pretty=short -u -L 2003,2005:Executor.cpp http://stackoverflow.com/quest ...

  4. 开源PLM软件Aras详解六 角色与用户以及权限

    在Aras中,角色(Identity),用户(Users),权限(Permissions),分别为3个ItemType,Permissions依赖与Identity,Identity可依赖与User. ...

  5. oc 单例

    单例模式: //static id _instace; // //+ (id)allocWithZone:(struct _NSZone *)zone //{ // static dispatch_o ...

  6. Xcode Custom Shortcut

    edit file "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources" add < ...

  7. 识别有效的IP地址和掩码并进行分类统计

    该题我的想法是把每一个ip看出一个整数,将读取得到的数据一一与给定的ip范围比较即可.另外本题应该注意的地方是scanf读取俩字符串的方法. 代码如下: #include<stdio.h> ...

  8. [[UIScreen mainScreen] bounds] 返回的屏幕尺寸不对

    在使用cocos2d-iphone 2.0生成项目的时候,用5s测试时全屏中上下一直有黑条,发现[[UIScreen mainScreen] bounds]返回的屏幕尺寸不是320*568的,而是32 ...

  9. 通过viewmodel找到view

    如何通过viewmodel找到view? 之前的做法是,在view加载时(Loaded),将view保存到viewmodel中,后来想想Caliburn-Micro,自带方法可以通过viewmodel ...

  10. RFC2119:表示要求的动词(转)

    RFC(Request For Comments)指的关于互联网标准的正式文件,它们的内容必须写得非常清楚. 表达的时候,必须严格区分哪些是"建议"(suggestion),哪些是 ...