LINQ to SQL Transact-SQL函数的用法

1. CASE WHEN ... THEN ...

linq用法:

var expr = from p in context.Products
            select new 
            { 
                商品ID = p.ProductID, 
                商品名称 = p.ProductName, 
                是否库存 = p.UnitsInStock > 0 ? "" : "" 
            };

sql用法:

SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName], 
    CASE WHEN ([Extent1].[UnitsInStock] > 0) THEN N'' ELSE N'' END AS [C1]
    FROM [dbo].[Product] AS [Extent1]

2.Distinct的用法

linq用法:

var expr = context.Products
    .Select(p => p.CategoryID)
    .Distinct();

sql用法:

SELECT 
    [Distinct1].[CategoryID] AS [CategoryID]
    FROM ( SELECT DISTINCT 
        [Extent1].[CategoryID] AS [CategoryID]
        FROM [dbo].[Product] AS [Extent1]
    )  AS [Distinct1]

3. Count的用法

linq用法:

var expr = context.Products.Count();

sql用法:

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Product] AS [Extent1]
    )  AS [GroupBy1]
posted @ 2019-01-29 16:39  Tim1027  阅读(246)  评论(0编辑  收藏  举报