zoukankan      html  css  js  c++  java
  • Linq group by

    1.简单形式:

    var q
    =  from p in db.Products  group p by p.CategoryID into g  select g;
    语句描述:Linq使用Group By按CategoryID划分产品。

    说明:from p
    in db.Products 表示从表中将产品对象取出来。group p by p.CategoryID into g表示对p按CategoryID字段归类。其结果命名为g,一旦重新命名,p的作用域就结束了,所以,最后select时,只能select g。

    2.最大值

    var q
    =  from p in db.Products  group p by p.CategoryID into g  select new {  g.Key,  MaxPrice = g.Max(p => p.UnitPrice)  };
    语句描述:Linq使用Group By和Max查找每个CategoryID的最高单价。

    说明:先按CategoryID归类,判断各个分类产品中单价最大的Products。取出CategoryID值,并把UnitPrice值赋给MaxPrice。

    3.最小值

    var q
    =  from p in db.Products  group p by p.CategoryID into g  select new {  g.Key,  MinPrice = g.Min(p => p.UnitPrice)  };
    语句描述:Linq使用Group By和Min查找每个CategoryID的最低单价。

    说明:先按CategoryID归类,判断各个分类产品中单价最小的Products。取出CategoryID值,并把UnitPrice值赋给MinPrice。

    4.平均值

    var q
    =  from p in db.Products  group p by p.CategoryID into g  select new {  g.Key,  AveragePrice = g.Average(p => p.UnitPrice)  };
    语句描述:Linq使用Group By和Average得到每个CategoryID的平均单价。

    说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的平均值。

    5.求和

    var q
    =  from p in db.Products  group p by p.CategoryID into g  select new {  g.Key,  TotalPrice = g.Sum(p => p.UnitPrice)  }; 

  • 相关阅读:
    python文件的读写操作
    python的基础函数以及一些特性
    iOS中导航栏动态隐藏的其中一种方式
    python中字典和数组的使用
    C语言-简介
    C语言-数据数据类型、变量与常量
    C语言-运算符与表达式
    weak 和 assign 的区别!!!
    iOS 审核因为“内购”被拒的解决方案!
    pod install -bash: pod: command not found
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1766698.html
Copyright © 2011-2022 走看看