Multiple Sort with Common Expression Elements

Edit 2019-01-31: Latest Solution

I followed the examples here and here to create a general view using expression expressions, but I cannot figure out how I should add a ThenBy clause or combine multiple columns for sorting in the callexpression method. Ideally, ThenBy should go before skipping, but it cannot, because it cannot see the orderby sentence that I made using the callexpression method. GridSortExpression is a Telerik class - it simply describes which column and in which direction the request should be sorted.

Can anyone shed some light? Here is what I have now:

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 
+6
source share
2 answers

Here's a very general way to sort properties without having to implement multiple sorting sorters for each slightly different view.

it will allow any number of Columns sortings (although you need to do a bit more to support different sorting directions)

The disadvantage is obviously not the most effective, although using calling delegates is much more efficient than using reflection. You also need to define the general function of the properties of objects that you can sort. By making this a more specific object, you can fix this aspect ...

 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 
+1
source

Are you trying to pretend that one day you can sort by 1 property, then another day the sort can be based on 2 or more properties?

If so, you may need to use reflection to get the number of properties, and then sort them based on their TYPE. This is an interesting idea if your goal is to sort based on any number of properties. However, what if one of these properties has a TYPE, then this is another class, then what?

I think you may need to look

Implements IComparable

to create your own sort procedure.

Here is a usage example here: →

http://msdn.microsoft.com/en-us/library/4d7sx9hd (VS.80) .aspx

However, this will be specific to each class you create.

0
source

Source: https://habr.com/ru/post/888660/


All Articles