LINQ OrderBy排序问题

发布于 2024-10-28 21:29:13 字数 1763 浏览 0 评论 0原文

我有一个 LINQ 查询:

bikersList = (From c In ngBikersDataContext.Reg_Bikers _
                        Order By c.L_Name _
                        Select New Bikers() With { _
                        .BikerID = c.BikerID, _
                        .F_Name = c.F_Name, _
                        .M_Name = c.M_Name, _
                        .L_Name = c.L_Name, _
                        .MyID = c.MyID, _
                        .Site = c.Site.GetValueOrDefault, _
                        .bk_Building = c.bk_Building, _
                        .bk_City = c.bk_City, _
                        .bk_Zip = c.bk_Zip.GetValueOrDefault, _
                        .bk_Phone = c.bk_phone, _
                        .email = c.email, _
                        .DeptZone = c.DeptZone, _
                        .QuartID = c.QuartID.GetValueOrDefault, _
                        .BikerDays = c.BikerDays.GetValueOrDefault, _
                        .BikerMiles = c.BikerMiles.GetValueOrDefault, _
                        .BikerTime = c.BikerTime.GetValueOrDefault, _
                        .BKLockID = c.BKLockID.GetValueOrDefault, _
                        .bk_Start_DT = c.bk_Start_DT, _
                        .bk_End_DT = c.bk_End_DT, _
                        .bk_Quarter = c.bk_Quarter.GetValueOrDefault, _
                        .bk_Year = c.bk_Year.GetValueOrDefault, _
                        .bk_Comments = c.bk_Comments, _
                        .IsActive = c.IsActive.GetValueOrDefault _
                        }).ToList()

这非常有效,并且可以根据 L_Name 进行排序。但我试图允许用户自己对 gridview 进行排序。所以我将 SortExpression 作为字符串传递。但我不知道如何将 SortExpression 合并到 LINQ 查询中。

我尝试过

Order By c. & SortExpression

但这没有用。

I have a LINQ Query:

bikersList = (From c In ngBikersDataContext.Reg_Bikers _
                        Order By c.L_Name _
                        Select New Bikers() With { _
                        .BikerID = c.BikerID, _
                        .F_Name = c.F_Name, _
                        .M_Name = c.M_Name, _
                        .L_Name = c.L_Name, _
                        .MyID = c.MyID, _
                        .Site = c.Site.GetValueOrDefault, _
                        .bk_Building = c.bk_Building, _
                        .bk_City = c.bk_City, _
                        .bk_Zip = c.bk_Zip.GetValueOrDefault, _
                        .bk_Phone = c.bk_phone, _
                        .email = c.email, _
                        .DeptZone = c.DeptZone, _
                        .QuartID = c.QuartID.GetValueOrDefault, _
                        .BikerDays = c.BikerDays.GetValueOrDefault, _
                        .BikerMiles = c.BikerMiles.GetValueOrDefault, _
                        .BikerTime = c.BikerTime.GetValueOrDefault, _
                        .BKLockID = c.BKLockID.GetValueOrDefault, _
                        .bk_Start_DT = c.bk_Start_DT, _
                        .bk_End_DT = c.bk_End_DT, _
                        .bk_Quarter = c.bk_Quarter.GetValueOrDefault, _
                        .bk_Year = c.bk_Year.GetValueOrDefault, _
                        .bk_Comments = c.bk_Comments, _
                        .IsActive = c.IsActive.GetValueOrDefault _
                        }).ToList()

This works great and sorts on L_Name. But I am trying to allow the user to sort the gridview themselves. So I am passing in the SortExpression as a string. But I don't know how to incorperate the SortExpression into the LINQ Query.

I tried

Order By c. & SortExpression

But that did not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-11-04 21:29:13

您应该查看 Linq 中称为动态查询的东西。

使用 LINQ 动态查询库

You should check out something called a Dynamic Query in Linq.

Using the LINQ Dynamic Query Library

神经大条 2024-11-04 21:29:13

以下文章讨论了使用排序表达式字符串通过 linq 进行动态排序:

http://www. codeproject.com/KB/recipes/Generic_Sorting.aspx

基本上,您需要手动构建表达式树。

(此代码来自上面的链接)

Public Function Sort(ByVal source As IEnumerable(Of T), _
                     ByVal sortBy As String, _
                     ByVal sortDirection As String) As IEnumerable(Of T)

    Dim param = Expression.Parameter(GetType(T), "item")

    Dim sortExpression = Expression.Lambda(Of Func(Of T, Object))_
    (Expression.Convert(Expression.[Property](param, sortBy), _
    GetType(Object)), param)

    Select Case sortDirection.ToLower
        Case "asc"
            Return source.AsQueryable().OrderBy(sortExpression)
        Case Else
            Return source.AsQueryable().OrderByDescending(sortExpression)
    End Select

End Function

Here's an article that talks about dynamic sorting with linq using a sortexpression string:

http://www.codeproject.com/KB/recipes/Generic_Sorting.aspx

Basically, you'll need to build the expression tree manually.

(this code is from the link above)

Public Function Sort(ByVal source As IEnumerable(Of T), _
                     ByVal sortBy As String, _
                     ByVal sortDirection As String) As IEnumerable(Of T)

    Dim param = Expression.Parameter(GetType(T), "item")

    Dim sortExpression = Expression.Lambda(Of Func(Of T, Object))_
    (Expression.Convert(Expression.[Property](param, sortBy), _
    GetType(Object)), param)

    Select Case sortDirection.ToLower
        Case "asc"
            Return source.AsQueryable().OrderBy(sortExpression)
        Case Else
            Return source.AsQueryable().OrderByDescending(sortExpression)
    End Select

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文