首页 > 数据库技术 > 详细

LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

时间:2014-10-25 20:00:05      阅读:389      评论:0      收藏:0      [点我收藏+]

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 (NLINQ to Object, NLINQ to ADO.NET, NLINQ 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 (NLINQ to Object, NLINQ to ADO.NET, NLINQ to XML))

LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

原文:http://www.cnblogs.com/libingql/p/4050500.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!