zoukankan      html  css  js  c++  java
  • [置顶]linq查询函数的应用

    最近做项目,用到了max,min,average等函数。sql server 的相信很多人会用,而linq的或许很多人比较陌生,下面是我从网上搜集的一个案例,关于linq的几个典型函数,供大家学习。

    A.min()函数
    min
    var categories =
      from p in db.Products
      group p by p.CategoryID into g
      select new {
        CategoryID = g.Key,
         CheapestProducts =
          from p2 in g
           where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
           select p2
      };

    B.max()函数

      //var q = db.Employees.Select(e => e.HireDate).Max();

    //var q = db.Products.Max(p => p.UnitsInStock);

    var categories =
      from p in db.Products
       group p by p.CategoryID into g
      select new {
         g.Key,
        MostExpensiveProducts =
          from p2 in g
          where p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
          select p2
      };


    C.average()函数

    var q = db.Products.Average(p => p.UnitPrice);
    var q = db.Orders.Select(o => o.Freight).Average();
    var categories =
      from p in db.Products
      group p by p.CategoryID into g
      select new {
        g.Key,
        ExpensiveProducts =
           from p2 in g
          where p2.UnitPrice > g.Average (p3 => p3.UnitPrice)
          select p2
       };

  • 相关阅读:
    leecode 91. 解码方法
    leecode 166. 分数到小数
    剑指 Offer 31. 栈的压入、弹出序列
    leecode 386. 字典序排数
    LeetCode 311 稀疏矩阵的乘法
    leecode 89. 格雷编码
    leecode 79. 单词搜索
    leecode 207. 课程表
    QT -- 解决:Error: Could not decode "*.cpp" with "UTF-8"
    VS+QT -- 没有PRO文件的问题
  • 原文地址:https://www.cnblogs.com/shihao/p/2176719.html
Copyright © 2011-2022 走看看