LINQ系列:LINQ to SQL Where条件

1. 单一条件查询

var expr = context.Products
    .Where(p => p.ProductName == "LINQ to SQL");
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent1].[UnitPrice] AS [UnitPrice], 
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Product] AS [Extent1]
    WHERE N'LINQ to SQL' = [Extent1].[ProductName]

2. 组合条件查询

var expr = context.Products
    .Where(p => p.ProductName == "LINQ to SQL" && p.UnitPrice > 10m);
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent1].[UnitPrice] AS [UnitPrice], 
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Product] AS [Extent1]
    WHERE (N'LINQ to SQL' = [Extent1].[ProductName]) AND ([Extent1].[UnitPrice] > cast(10 as decimal(18)))

3. Like查询

var expr = from p in context.Products
           where p.ProductName.StartsWith("LINQ")
           select new { p.ProductID, p.ProductName };
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName]
    FROM [dbo].[Product] AS [Extent1]
    WHERE [Extent1].[ProductName] LIKE N'LINQ%'
var expr = from p in context.Products
           where p.ProductName.EndsWith("LINQ")
           select new { p.ProductID, p.ProductName };
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName]
    FROM [dbo].[Product] AS [Extent1]
    WHERE [Extent1].[ProductName] LIKE N'%LINQ'
var expr = from p in context.Products
           where p.ProductName.Contains("LINQ")
           select new { p.ProductID, p.ProductName };
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName]
    FROM [dbo].[Product] AS [Extent1]
    WHERE [Extent1].[ProductName] LIKE N'%LINQ%'

 

posted @ 2014-10-24 15:53  libingql  阅读(12995)  评论(0编辑  收藏  举报