LINQ系列:LINQ to SQL Where條件

libingql發表於2014-10-24

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%'

 

相關文章