LINQ排序问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过使用 System.Linq.Expressions.Expression。
这里是一个解释相同内容的示例
you can do this by dynamically constructing the linq expression using System.Linq.Expressions.Expression.
Here is an example which explains the same