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

    取相交项

     

    描述:查询城市是A打头的顾客和城市包含A的顾客的交集,并按照顾客名字排序

    查询句法:

    var 取相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Intersect

                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    对应SQL

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

    FROM (

        SELECT DISTINCT [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]

        ) AS [t1]

    WHERE (EXISTS(

        SELECT NULL AS [EMPTY]

        FROM [dbo].[Customers] AS [t2]

        WHERE ([t1].[CustomerID] = [t2].[CustomerID]) AND ([t2].[ContactName] LIKE @p0)

        )) AND ([t1].[City] LIKE @p1)

    ORDER BY [t1].[ContactName]

    -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%]

    -- @p1: Input String (Size = 3; Prec = 0; Scale = 0) [%A%]

     

    排除相交项

     

    描述:查询城市包含A的顾客并从中删除城市以A开头的顾客,并按照顾客名字排序

    查询句法:

    var 排除相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Except

                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    对应SQL

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

    FROM (

        SELECT DISTINCT [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]

        ) AS [t1]

    WHERE (NOT (EXISTS(

        SELECT NULL AS [EMPTY]

        FROM [dbo].[Customers] AS [t2]

        WHERE ([t1].[CustomerID] = [t2].[CustomerID]) AND ([t2].[ContactName] LIKE @p0)

        ))) AND ([t1].[City] LIKE @p1)

    ORDER BY [t1].[ContactName]

    -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%]

    -- @p1: Input String (Size = 3; Prec = 0; Scale = 0) [%A%]

     

    子查询

    描述:查询订单数超过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;

    对应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 EXISTS(

        SELECT NULL AS [EMPTY]

        FROM (

            SELECT COUNT(*) AS [value], [t1].[CustomerID]

            FROM [dbo].[Orders] AS [t1]

            GROUP BY [t1].[CustomerID]

            ) AS [t2]

        WHERE ([t2].[CustomerID] = [t0].[CustomerID]) AND ([t2].[value] > @p0)

        )

    -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [5]

  • 相关阅读:
    Codeforces Round #687 A. Prison Break
    最小生成树自用笔记(Kruskal算法+prim算法)
    Codeforces Round #686 (Div. 3)(A->D)(模拟,vector,数学)
    Acwing 852. spfa判断负环
    Linux内核分析_课程学习总结报告
    结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程
    深入理解系统调用
    基于mykernel 2.0编写一个操作系统内核
    何评测一个软件工程师的计算机网络知识水平与网络编程技能水平?——参考试题
    TCP三次握手Linux源码解析
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1749088.html
Copyright © 2011-2022 走看看