

New OrderSpecification(p=>p.Name, true), // then order by Name descendinglyįoreach (var p in output) Console. Chaining includes only work if the first include call is from a DbQuery. LINQ to Entities converts Language-Integrated Queries (LINQ) queries to command tree queries, executes the queries against the Entity Framework, and returns objects that can be used by both the Entity Framework and LINQ. New OrderSpecification(p=>p.Id), // Order by Id first, Var output = products.Sort(new OrderSpecification Query = spec.IsDescending ? temp!.ThenByDescending(spec.Selector) : temp!.ThenBy(spec.Selector) Query = spec.IsDescending ? query.OrderByDescending(spec.Selector) : query.OrderBy(spec.Selector) Public static IQueryable Sort(this IQueryable query, OrderSpecification specs) where T : BaseEntity Date Comparison In Entity Framework Linq Query.In some cases, you dont know until run time how many predicates you have to apply to source elements in the. Public OrderSpecification(Expression> selector, bool isDescending = false) If you also want to tackle that, you'll have to walk through the complete tree which might just add that overhead you don't want :) REMARK These extension methods only check the previous expression in the expression tree to determine wether to use OrderBy or ThenBy, no other expressions are allowed in-between. Query = query.AppendOrderBy(l => l.Category)

Query = query.AppendOrderBy(l => l.Color) Query = query.AppendOrderBy(l => l.Type) You may want to look at a DataTable Compute () method which does use strings to build commands that. Query = query.AppendOrderBy(l => l.RegionCode)
#Ef chain linq commands code#
The code in the question could then be refactored to: foreach (string sort in data.SortParams) Finally, you’ll learn what an EF Core query, with its LINQ part, looks like.
Public static IOrderedQueryable AppendOrderByDescending(this IQueryable query, Expression> keySelector) The IQueryable? ((IOrderedQueryable) query).ThenBy(keySelector) Public static IOrderedQueryable AppendOrderBy(this IQueryable query, Expression> keySelector) I've created these extension methods to tackle an identical problem as stated in the question: public static class QueryableExtensions
