码迷,mamicode.com
首页 > 数据库 > 详细

LINQ系列:LINQ to SQL Take/Skip

时间:2014-10-26 22:52:05      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   使用   strong   sp   div   on   

1. Take

var expr = context.Products
    .Take(10);
var expr = (from p in context.Products
            select p)
            .Take(10);
SELECT TOP (10) 
    [c].[ProductID] AS [ProductID], 
    [c].[CategoryID] AS [CategoryID], 
    [c].[ProductName] AS [ProductName], 
    [c].[UnitPrice] AS [UnitPrice], 
    [c].[UnitsInStock] AS [UnitsInStock], 
    [c].[Discontinued] AS [Discontinued]
    FROM [dbo].[Product] AS [c]

2. Skip

  在使用Skip方法之前,需要先有OrderBy排序。

var expr = context.Products
    .Select(p => new { p.ProductID, p.ProductName })
    .OrderBy(p => p.ProductID)
    .Skip(10);
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName]
    FROM ( SELECT [Extent1].[ProductID] AS [ProductID], [Extent1].[ProductName] AS [ProductName], row_number() OVER (ORDER BY [Extent1].[ProductID] ASC) AS [row_number]
        FROM [dbo].[Product] AS [Extent1]
    )  AS [Extent1]
    WHERE [Extent1].[row_number] > 10
    ORDER BY [Extent1].[ProductID] ASC

3. 分页查询Take/Skip

var expr = context.Products
    .Select(p => new { p.ProductID, p.ProductName })
    .OrderBy(p => p.ProductID)
    .Skip(10)
    .Take(10);
SELECT TOP (10) 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[ProductName] AS [ProductName]
    FROM ( SELECT [Extent1].[ProductID] AS [ProductID], [Extent1].[ProductName] AS [ProductName], row_number() OVER (ORDER BY [Extent1].[ProductID] ASC) AS [row_number]
        FROM [dbo].[Product] AS [Extent1]
    )  AS [Extent1]
    WHERE [Extent1].[row_number] > 10
    ORDER BY [Extent1].[ProductID] ASC

LINQ系列:LINQ to SQL Take/Skip

标签:style   blog   color   ar   使用   strong   sp   div   on   

原文地址:http://www.cnblogs.com/libingql/p/4052842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!