gpt4 book ai didi

asp.net - 具有通用成员表达式的多个排序

转载 作者:行者123 更新时间:2023-12-04 16:01:33 26 4
gpt4 key购买 nike

2019-01-31 编辑:Latest solution

我已经按照示例 herehere使用成员表达式创建通用排序,但我不知道应该如何添加“ThenBy”子句,或者在方法调用表达式中组合多个列进行排序。理想情况下,ThenBy 应该在 skip 之前,但它不能,因为它看不到我使用 methodcallexpression 制作的 orderby 子句。 GridSortExpression 是一个 Telerik 类——它只描述了查询应该被排序的列和方向。

任何人都可以解释一下吗?这是我现在所拥有的:

Dim exp As Expressions.Expression(Of Func(Of Product_Catalog, Boolean)) = PredicateBuilder.True(Of Product_Catalog)()
exp = exp.And(Function(e) e.Chapter_Price > 30)
Dim sortExpression As New List(Of GridSortExpression)({New GridSortExpression() With {.SortOrder = GridSortOrder.Descending, .FieldName = "Id"}})
If sortExpression.Count = 0 Then
catalogList = con.Product_Catalogs.AsExpandable.Where(exp).OrderBy(Function(o) o.Item_Type).ThenBy(Function(o) o.Item_Description).Skip(startRowIndex).Take(maximumRows).ToList
Else
Dim param As ParameterExpression = Expression.Parameter(GetType(Product_Catalog), String.Empty)
Dim prop As MemberExpression = Expression.PropertyOrField(param, sortExpression(0).FieldName)
Dim sort As LambdaExpression = Expression.Lambda(prop, param)
Dim source = con.Product_Catalogs.AsExpandable.Where(exp)
Dim resultExp As MethodCallExpression
resultExp = Expression.[Call](GetType(Queryable), "OrderBy" & If(sortExpression(0).SortOrder = GridSortOrder.Descending, "Descending", ""), _
New Type() {GetType(Product_Catalog), prop.Type}, con.Product_Catalogs.AsExpandable.Where(exp).Expression, Expression.Quote(sort))

catalogList = source.Provider.CreateQuery(Of Product_Catalog)(resultExp).Skip(startRowIndex).Take(maximumRows).ToList
End If

最佳答案

这是一种非常通用的方式来进行属性排序,而不必为每个稍微不同的排序实现多个排序比较器。

它将允许任意数量的排序“列”(尽管您需要稍微增强以支持不同的排序方向)

缺点是它显然不是最有效的,尽管使用调用委托(delegate)比使用反射更有效。您还需要定义您可能想要排序的对象的属性的一般功能。通过使其更具体的对象,您可以修复该方面......

Public Class PropertySortComparer
Implements IComparer

Public Delegate Function GetProp(ByVal obj As Object) As Object
Public SortProps As New List(Of GetProp)

Public Sub New(ParamArray SortMethods() As GetProp)
Me.SortProps.AddRange(SortMethods)
End Sub

Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
For Each gp As GetProp In SortProps
Dim xVal As Object = gp.Invoke(x)
Dim yVal As Object = gp.Invoke(y)
If xVal > yVal Then
Return 1
ElseIf xVal < yVal Then
Return -1
Else
'next loop does next property
End If
Next
Return 0
End Function
End Class

Public Module module1
Sub test()
Dim buffer As New List(Of Rectangle)
buffer.Add(New Rectangle(34, 55, 40, 30))
buffer.Add(New Rectangle(34, 55, 45, 38))
buffer.Add(New Rectangle(34, 56, 46, 30))
buffer.Add(New Rectangle(34, 70, 45, 30))

Dim Lst() As Rectangle = buffer.ToArray
Array.Sort(Lst, New PropertySortComparer(AddressOf Left, AddressOf Top, AddressOf Widht))
'Lst is now sorted by Left, Top, Width
End Sub

Public Function Left(r As Object) As Object
Return r.Left
End Function

Public Function Top(r As Object) As Object
Return r.Top
End Function

Public Function Widht(r As Object) As Object
Return r.Width
End Function

End Module

关于asp.net - 具有通用成员表达式的多个排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com