Monday, November 22, 2010

Partitioning Operators

Partitioning Operators
As mentioned the partitioning operators enable paging operations and Take(n) emulates T - SQL ’ s TOP(n) operator.
Here ’ s generic code to return a specific page of a given size:
C# 3.0
int pageSize
int pageNum
(from s in dataSource select s).Skip((pageNum -1) * pageSize)
.Take(pageSize)
VB 9.0
Dim PageSize As Integer
Dim PageNum As Integer
From s In DataSource
Select s
Skip (PageNum -1) * PageSize)
Take PageSize
The TakeWhile and SkipWhile operators have the index overload, which enables the use of the index
value in your paging or positioning logic; the Take and Skip operators aren ’ t overloaded.
Take
Take returns from the source sequence the number of contiguous elements specified by count .
C# 3.0
public static IEnumerable < TSource > Take < TSource > (
IEnumerable < TSource > source,
int count
)
VB 9.0
Public Shared Function Take(Of TSource) ( _
source As IEnumerable(Of TSource), _
count As Integer _
) As IEnumerable(Of TSource)
Skip
In LINQ for Objects, Skip bypasses in the source enumeration the number of elements specified by
count and returns the remaining source elements, if any.
C# 3.0
public static IEnumerable < TSource > Skip < TSource > (
IEnumerable < TSource > source,
int count
)
VB 9.0
Public Shared Function Skip(Of TSource) ( _
source As IEnumerable(Of TSource), _
count As Integer _
) As IEnumerable(Of TSource)
To minimize the number of rows returned from the database server, LINQ to SQL ’ s Skip implementation
behaves differently. Chapter 5 ’ s “ Paging in LINQ to SQL ” and “ Paging with the LINQDataSource
Control ” sections describe the T - SQL statements sent by Take and Skip to SQL Server 200x.