zoukankan      html  css  js  c++  java
  • Linq to sql(四):查询句法(六)

    in操作

     

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

    查询句法:

            var in操作 = from c in ctx.Customers

                        where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City)

                        select c;

    对应SQL

           SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]

    FROM [dbo].[Customers] AS [t0]

    WHERE [t0].[City] IN (@p0, @p1, @p2)

    -- @p0: Input String (Size = 11; Prec = 0; Scale = 0) [Brandenburg]

    -- @p1: Input String (Size = 5; Prec = 0; Scale = 0) [Cowes]

    -- @p2: Input String (Size = 7; Prec = 0; Scale = 0) [Stavern]

     

    join

     

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

    查询句法:

    var innerjoin = from p in ctx.Products

                            join c in ctx.Categories

                            on p.CategoryID equals c.CategoryID

                            select p.ProductName;

    对应SQL

    SELECT COUNT(*) AS [value]

    FROM [dbo].[Products] AS [t0]

    INNER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])

     

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

    查询句法:

    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;

    对应SQL

    SELECT COUNT(*) AS [value]

    FROM [dbo].[Products] AS [t0]

    LEFT OUTER JOIN [dbo].[Categories] AS [t1] ON [t0].[CategoryID] = ([t1].[CategoryID])

     

           你可能会很奇怪,原先很复杂的SQL使用查询句法会很简单(比如按照条件分组)。

    但是原先觉得很好理解的SQL使用查询句法会觉得很复杂(比如连接查询)。其实,

    我们还可以通过其它方式进行连接操作,在以后说DataLoadOptions类型的时候会再说。

    虽然Linq to sql已经非常智能了,但是对于非常复杂的查询还是建议通过存储过程实现,

    下次讲解如何调用存储过程。

  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1749087.html
Copyright © 2011-2022 走看看