zoukankan      html  css  js  c++  java
  • Linq to SQL 语法整理(子查询 & in操作 & join )

    子查询

    描述:查询订单数超过5的顾客信息
    查询句法:

    var 子查询 = from c in ctx.Customers
                       where
                           (from o in ctx.Orders group o by o.CustomerID into o where o.Count() > 5 select o.Key).Contains(c.CustomerID)
                       select c;
    

    in 操作

    描述:查询指定城市中的客户
    查询句法:

    var in操作 = from c in ctx.Customers
                        wherenewstring[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City)
                        select c;
    

    Join

    描述:内连接,没有分类的产品查询不到
    查询句法:

    var innerjoin = from p in ctx.Products
                            join c in ctx.Categories
                            on p.CategoryID equals c.CategoryID
                            select p.ProductName;
    

    描述:外连接,没有分类的产品也能查询到
    查询句法:

    var leftjoin = from p in ctx.Products
                           join c in ctx.Categories
                           on p.CategoryID equals c.CategoryID
                           into pro
                           from x in pro.DefaultIfEmpty()
                           select p.ProductName;
    

    简单形式:

    var q =
        from p in db.Products
        group p by p.CategoryID into g
        select g;
    

    语句描述:使用Group By按CategoryID划分产品。

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

    var q =
        from p in db.Products
        group p by p.CategoryID;
    

    Select匿名类:

    var q =
        from p in db.Products
        group p by p.CategoryID into g
        select new { CategoryID = g.Key, g }; 
    

    说明:在这句LINQ语句中,有2个property:CategoryID和g。这个匿名类,其实质是对返回结果集重新进行了包装。把g的property封装成一个完整的分组。

    如果想遍历某匿名类中所有记录,要这么做:

    foreach (var gp in q)
    {
        if (gp.CategoryID == 2)
        {
            foreach (var item in gp.g)
            {
                //do something
            }
        }
    }
    

    最大值

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

    语句描述:使用Group By和Max查找每个CategoryID的最高单价。

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

    最小值

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

    语句描述:使用Group By和Min查找每个CategoryID的最低单价。

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

    平均值

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

    语句描述:使用Group By和Average得到每个CategoryID的平均单价。
    说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的平均值。

    求和

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

    语句描述:使用Group By和Sum得到每个CategoryID 的单价总计。
    说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的总和。

    计数

    var q =
        from p in db.Products
        group p by p.CategoryID into g
        select new {
            g.Key,
            NumProducts = g.Count()
        };
    

    语句描述:使用Group By和Count得到每个CategoryID中产品的数量。
    说明:先按CategoryID归类,取出CategoryID值和各个分类产品的数量。

    带条件计数

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

    语句描述:使用Group By和Count得到每个CategoryID中断货产品的数量。

    说明:先按CategoryID归类,取出CategoryID值和各个分类产品的断货数量。 Count函数里,使用了Lambda表达式,Lambda表达式中的p,代表这个组里的一个元素或对象,即某一个产品。

    Where限制

    var q =
        from p in db.Products
        group p by p.CategoryID into g
        where g.Count() >= 10
        select new {
            g.Key,
            ProductCount = g.Count()
        };
    

    语句描述:根据产品的―ID分组,查询产品数量大于10的ID和产品数量。这个示例在Group By子句后使用Where子句查找所有至少有10种产品的类别。

    说明:在翻译成SQL语句时,在最外层嵌套了Where条件。

    多列(Multiple Columns)

    var categories =
        from p in db.Products
        group p by new
        {
            p.CategoryID,
            p.SupplierID
        }
            into g
            select new
                {
                    g.Key,
                    g
                };
    

    语句描述:使用Group By按CategoryID和SupplierID将产品分组。

    说明:既按产品的分类,又按供应商分类。在by后面,new出来一个匿名类。这里,Key其实质是一个类的对象,Key包含两个Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍历CategoryID的值。

    表达式(Expression)

    var categories =
        from p in db.Products
        group p by new { Criterion = p.UnitPrice > 10 } into g
        select g;
    

    语句描述:使用Group By返回两个产品序列。第一个序列包含单价大于10的产品。第二个序列包含单价小于或等于10的产品。

    说明:按产品单价是否大于10分类。其结果分为两类,大于的是一类,小于及等于为另一类。

    Exists/In/Any/All/Contains操作符
    适用场景:用于判断集合中元素,进一步缩小范围。

    Any
    说明:用于判断集合中是否有元素满足某一条件;不延迟。(若条件为空,则集合只要不为空就返回True,否则为False)。有2种形式,分别为简单形式和带条件形式。

  • 相关阅读:
    Git出现error: Your local changes to the following files would be overwritten by merge: ... Please, commit your changes or stash them before you can merge.的问题解决(Git代码冲突)
    JDK内置工具jstack(Java Stack Trace)(转)
    Java 5/Java 6/Java7/Java 8新特性收集
    Linux使用screen实现关闭ssh连接的情况下,让程序继续在后台运行
    Linux出现cannot create temp file for here-document: No space left on device的问题解决
    解决树莓派8G的SD卡只能识别3.3G,SD卡扩容
    Windows下拷贝Linux的文件到本地(Putty)
    Linux下运行Java项目时,出现No X11 DISPLAY variable was set, but this program performed an operation which requires it.的问题解决
    Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决
    Ubuntu下安装Maven
  • 原文地址:https://www.cnblogs.com/codedisco/p/13083068.html
Copyright © 2011-2022 走看看