I have a problem on using LinqToSQL with paging + dynamic sorting. These are my sample code.
Using db As New MyDataContext(connectionString)
db.Log = new DebuggerWritter
Dim result = db.User.OrderBy(Function(u) u.UserId)
result = result.Skip((pageNo - 1) * pageSize).Take(pageSize)
End Using
This is the SQL script generated by LINQToSQL, which is only retrieve certain row of records.
SELECT [t1].[UserId], [t1].[UserName]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[UserId]) AS [ROW_NUMBER], [t0].[UserId], [t0].[UserName]
FROM [dbo].[User] AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]
But if i implemented dynamic sort,
Using db As New MyDataContext(connectionString)
db.Log = new DebuggerWritter
Dim result = db.User
For Each s In sortExpressions
Dim expression As Func(Of User, Object) = Function(u) u.[GetType]().GetProperty(s.propertyName).GetValue(u, Nothing)
Select Case s.SortOrder
Case SortExpression.SortDirection.Ascending
result = result.OrderBy(expression).AsQueryable
Case SortExpression.SortDirection.Descending
result = result.OrderByDescending(expression).AsQueryable
End Select
Next
result = result.Skip((pageNo - 1) * pageSize).Take(pageSize)
End Using
This is the SQL script generated this time,
SELECT [t0].[UserId], [t0].[UserName]
FROM [dbo].[User] AS [t0]
How come the paging control script is not generated and the sorting is gone?? Is that the way i implement dynamic sorting is wrong? or LinqToSQL did not support paging + dynamic sorting?? Help!!!