我在使用LinqToSQL進行分頁+動態排序時遇到了問題。這些是我的示例代碼。LinqToSQL +分頁+動態排序?
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
這是由LINQToSQL生成的SQL腳本,它只檢索特定行記錄。
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]
但是,如果我執行的動態排序,
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
這是此時產生的SQL腳本,
SELECT [t0].[UserId], [t0].[UserName]
FROM [dbo].[User] AS [t0]
怎麼來尋呼控制腳本不產生和排序是走了?那我實現動態排序的方式是錯誤的嗎?或者LinqToSQL不支持分頁+動態排序? 幫助!!!
謝謝,它真的爲我工作。 – Kevin 2010-03-15 02:32:45