zoukankan      html  css  js  c++  java
  • LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

    1. Any

      返回没有Product的Category

    var expr = from c in context.Categories
                where !c.Products.Any()
                select c;
    SELECT 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[CategoryName] AS [CategoryName]
        FROM [dbo].[Category] AS [Extent1]
        WHERE  NOT EXISTS (SELECT 
            1 AS [C1]
            FROM [dbo].[Product] AS [Extent2]
            WHERE [Extent1].[CategoryID] = [Extent2].[CategoryID]
        )
    var expr = from c in context.Categories
                where !c.Products.Any(p => p.UnitPrice > 10m)
                select c;
    SELECT 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[CategoryName] AS [CategoryName]
        FROM [dbo].[Category] AS [Extent1]
        WHERE  NOT EXISTS (SELECT 
            1 AS [C1]
            FROM [dbo].[Product] AS [Extent2]
            WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[UnitPrice] > cast(10 as decimal(18)))
        )

    2. All

    var expr = from c in context.Categories
               where c.Products.All(p => p.Discontinued)
               select c;
    SELECT 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[CategoryName] AS [CategoryName]
        FROM [dbo].[Category] AS [Extent1]
        WHERE  NOT EXISTS (SELECT 
            1 AS [C1]
            FROM [dbo].[Product] AS [Extent2]
            WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[Discontinued] <> cast(1 as bit))
        )

    3. Contains

    var expr = from p in context.Products
                where new string[] 
                { 
                    "LINQ to Object",
                    "LINQ to ADO.NET", 
                    "LINQ to XML" 
                }
                .Contains(p.ProductName)
                select p;
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[ProductName] AS [ProductName], 
        [Extent1].[UnitPrice] AS [UnitPrice], 
        [Extent1].[UnitsInStock] AS [UnitsInStock], 
        [Extent1].[Discontinued] AS [Discontinued]
        FROM [dbo].[Product] AS [Extent1]
        WHERE [Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML')
    var expr = from p in context.Products
                where !(new string[] 
                { 
                    "LINQ to Object",
                    "LINQ to ADO.NET", 
                    "LINQ to XML" 
                })
                .Contains(p.ProductName)
                select p;
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[ProductName] AS [ProductName], 
        [Extent1].[UnitPrice] AS [UnitPrice], 
        [Extent1].[UnitsInStock] AS [UnitsInStock], 
        [Extent1].[Discontinued] AS [Discontinued]
        FROM [dbo].[Product] AS [Extent1]
        WHERE  NOT ([Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML'))
  • 相关阅读:
    多测师讲解unittest介绍及自动化测试实现流程_高级讲师肖sir
    多测师讲解常用的测试工具分为10类_高级讲师肖sir
    多测师讲解接口测试 _面试题003_高级讲师肖sir
    多测师接口测试 --常见的接口面试题目002---高级讲师肖sir
    Linux的mysql搭建
    学完爬虫的笔记
    postman测试实例--断言
    jemeter安装步骤
    selenium的显示等待和隐式等待区别
    loadrunner录制不了浏览器
  • 原文地址:https://www.cnblogs.com/libingql/p/4050500.html
Copyright © 2011-2022 走看看