Set key inside order by clause in linq dynamically - c#

I need to set the key in the OrderBy clause using linq , where the key is a property of an object. I only have the property name with me. How can i set the property dynamically.
class Obj
{
string Level
{
get;
set;
}
}
List<Obj> objList;
objList.OrderByDescending(x => x.Level); => "here i only have property name."
Hope any one can help
Thanks
Sunil

You can do this by Extension Methods and Creating Expression Trees dynamically like
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> enumerable, string sortColumn)
{
var param = Expression.Parameter(typeof(T), "x");
var mySortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, sortColumn), param);
return enumerable.OrderByDescending(mySortExpression);;
}
and for OrderBy
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> enumerable, string sortColumn)
{
var param = Expression.Parameter(typeof(T), "x");
var mySortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, sortColumn), param);
return enumerable.OrderBy(mySortExpression);;
}
and you can combine these two in one like
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> enumerable, string sortColumn, string direction)
{
var param = Expression.Parameter(typeof(T), "x");
var mySortExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, sortColumn), param);
IOrderedQueryable<T> iQuery;
switch(direction)
{
case "desc":
iQuery = enumerable.OrderByDescending(mySortExpression);
break;
case "asc":
default :
iQuery = enumerable.OrderBy(mySortExpression);
break;
}
return iQuery;
}
Then you can call it like objList.OrderBy("Level","asc") //For Ascending
objList.OrderBy("Level","desc") //For Descending
Hope It will help

Related

C# EF Core reflection order

When I have queryable such as
var query = dbSet.AsQueryable();
and I would like to dynamically set orderBy using reflection
var orderBy = "School.Name"
query = query.OrderBy("School.Name");
var data = await query.ToListAsync()
I do have extension to support order like this using reflection as following:
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
{
return source.OrderBy(ToLambda<T>(propertyName));
}
private static Expression<Func<T, object>> ToLambda<T>(string propertyName)
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter, propertyName);
var propAsObject = Expression.Convert(property, typeof(object));
return Expression.Lambda<Func<T, object>>(propAsObject, parameter);
}
Problem is to create lambda for inner object property such as School.Name. This solution works for Name attribute on Dbset object, but not on joined.
Goal is to modify ToLambda method to support joinned or inners properties.
using
query.OrderBy("School.Name");
// instead of
query.OrderBy(m => m.School.Name );
I have solved it as following:
private static Expression<Func<T, object>> ToLambda<T>(string propertyName)
{
if (propertyName.Contains('.'))
{
var parameter = Expression.Parameter(typeof(T));
var property = NestedProperty(parameter, propertyName.Split('.'));
var propAsObject = Expression.Convert(property, typeof(object));
return Expression.Lambda<Func<T, object>>(propAsObject, parameter);
}
else
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter, propertyName.ToLower());
var propAsObject = Expression.Convert(property, typeof(object));
return Expression.Lambda<Func<T, object>>(propAsObject, parameter);
}
}
private static MemberExpression NestedProperty(Expression propertyHolder, params string[] propertyPath)
{
MemberExpression memberExpression = Expression.Property(propertyHolder, propertyPath[0]);
foreach (var member in propertyPath.Skip(1))
{
memberExpression = Expression.Property(memberExpression, member);
}
return memberExpression;
}

LINQ method, which Dynamically ordering data by column name I send

What I want to do?
I want to create method, which Dynamically ordering data by column name I send.
First I created a project for testing. I see the project works super.
Test Project:
PropertyInfo pinfo = typeof(MockData).GetProperty(orderColumn);
switch (orderDirection)
{
case "asc":
mockDataList = q.OrderBy(o => pinfo.GetValue(o, null)).Skip(start).Take(length).ToList();
break;
case "desc":
mockDataList = q.OrderByDescending(o => pinfo.GetValue(o, null)).Skip(start).Take(length).ToList();
break;
}
After, I applied things, which I learned to my essential project that way:
Essential Project:
public IQueryable<T> RefactoringQuerybyPagination<T>(DataTablesRequestModel dataTablesRequestModel, IQueryable<T> query)
{
PropertyInfo pinfo = typeof(T).GetProperty(dataTablesRequestModel.OrderColumn);
switch (dataTablesRequestModel.OrderDirection)
{
case "asc":
query = query.OrderBy(o => pinfo.GetValue(o, null) != null).ThenBy(o => pinfo.GetValue(o, null));
break;
case "desc":
query = query.OrderByDescending(o => pinfo.GetValue(o, null) != null).ThenBy(o => pinfo.GetValue(o, null));
break;
}
query = query.Skip(dataTablesRequestModel.Start);
query = query.Take(dataTablesRequestModel.Length);
var test = query.ToList();
return query;
}
But it doesn't work and it gives me an error ("could not be translated...")
Solution I tried
public static IQueryable<T> OrderBy<T>(this IQueryable<T> items, string propertyName)
{
var typeOfT = typeof(T);
var parameter = Expression.Parameter(typeOfT, "parameter");
var propertyType = typeOfT.GetProperty(propertyName).PropertyType;
var propertyAccess = Expression.PropertyOrField(parameter, propertyName);
var orderExpression = Expression.Lambda(propertyAccess, parameter);
var expression = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { typeOfT, propertyType }, items.Expression, Expression.Quote(orderExpression));
return items.Provider.CreateQuery<T>(expression);
}
It works but I can't manipulate expression. (like o = > o.Column.HasValue).
All day, I worked on this, I'm really tired. Can anyone help me?
Try to create a QueryableExtensions with the the following code:
//required using System.Linq;
//required using System.Linq.Expressions;
public static class QueryableExtensions
{
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string columnName, bool isAscending = true)
{
if (String.IsNullOrEmpty(columnName))
{
return source;
}
ParameterExpression parameter = Expression.Parameter(source.ElementType, "");
MemberExpression property = Expression.Property(parameter, columnName);
LambdaExpression lambda = Expression.Lambda(property, parameter);
string methodName = isAscending ? "OrderBy" : "OrderByDescending";
Expression methodCallExpression = Expression.Call(typeof(Queryable), methodName,
new Type[] { source.ElementType, property.Type },
source.Expression, Expression.Quote(lambda));
return source.Provider.CreateQuery<T>(methodCallExpression);
}
Then, in the LINQ statement, we could use the above method to sort the records:
public IActionResult CategoryIndex()
{
//CategoryName Descending
var result1 = _context.Categories.OrderBy("CategoryName", false).Select(c=>c.CategoryName).ToArray();
//CategoryID Descending
var result2 = _context.Categories.OrderBy("CategoryID", false).Select(c => c.CategoryID).ToArray();
return View();
}
The screenshot as below:
You can use the Dynamic LINQ to achieve what you are trying to do in a lot easier and simplified way. You can find the NuGet package here - System.Linq.Dynamic.Core
From your code I'm assuming your scenario does not involve sorting/ordering on multiple columns. If so, you can create an extension method like below -
// you'll need to add this
// using System.Linq.Dynamic.Core;
public static class IQueryableExtension
{
public static IQueryable<T> ApplyOrder<T>(this IQueryable<T> source, string column, string direction)
{
if (string.IsNullOrWhiteSpace(column))
return source;
var pinfo = typeof(T).GetProperty(column);
if (pinfo == null)
return source;
var order = (direction == "desc") ? $"{column} desc" : column;
return source.OrderBy(order);
}
}
You can use it from your existing code like -
public IQueryable<T> RefactoringQuerybyPagination<T>(DataTablesRequestModel dataTablesRequestModel, IQueryable<T> query)
{
// here is the call
query = query.ApplyOrder(dataTablesRequestModel.OrderColumn, dataTablesRequestModel.OrderDirection);
query = query.Skip(dataTablesRequestModel.Start);
query = query.Take(dataTablesRequestModel.Length);
var test = query.ToList();
return query;
}

Convert Expression<Func<T, TProperty>> to Expression<Func<object, object>> and vice versa

Is there a way to convert the property selector Expression<Func<T, TProperty>> to Expression<Func<object, object>> and vice versa? I already know how to convert to Expression<Func<T, object>> using...
Expression<Func<T, TProperty>> oldExp;
Expression.Lambda<Func<T, object>>(Expression.Convert(oldExp.Body, typeof(object)), oldExp.Parameters);
... but I need to effectively cast both, the argument and the result of the function, not just e.g replace them with a ExpressionVisitor because they need to be casted back later.
You were correct that you need to use an ExpressionVisitor and ExpressionConvert.
Here are the two methods that you asked for (as well as some support methods):
public Expression<Func<object, object>> ConvertToObject<TParm, TReturn>(Expression<Func<TParm, TReturn>> input)
{
var parm = Expression.Parameter(typeof(object));
var castParm = Expression.Convert(parm, typeof(TParm));
var body = ReplaceExpression(input.Body, input.Parameters[0], castParm);
body = Expression.Convert(body, typeof(object));
return Expression.Lambda<Func<object, object>>(body, parm);
}
public Expression<Func<TParm, TReturn>> ConvertBack<TParm, TReturn>(Expression<Func<object, object>> input)
{
var parm = Expression.Parameter(typeof(TParm));
var castParm = Expression.Convert(parm, typeof(object));
var body = ReplaceExpression(input.Body, input.Parameters[0], castParm);
body = Expression.Convert(body, typeof(TReturn));
return Expression.Lambda<Func<TParm, TReturn>>(body, parm);
}
Expression ReplaceExpression(Expression body, Expression source, Expression dest)
{
var replacer = new ExpressionReplacer(source, dest);
return replacer.Visit(body);
}
public class ExpressionReplacer : ExpressionVisitor
{
Expression _source;
Expression _dest;
public ExpressionReplacer(Expression source, Expression dest)
{
_source = source;
_dest = dest;
}
public override Expression Visit(Expression node)
{
if (node == _source)
return _dest;
return base.Visit(node);
}
}
Sample usage:
Expression<Func<Customer, string>> expression = c => c.Name;
var convertedExpression = ConvertToObject<Customer, string>(expression);
var backExpression = ConvertBack<Customer, string>(convertedExpression);
Of course we can replace the original two methods with one method with more type parameters:
public Expression<Func<TTargetParm, TTargetReturn>> ConvertGeneric<TParm, TReturn, TTargetParm, TTargetReturn>(Expression<Func<TParm, TReturn>> input)
{
var parm = Expression.Parameter(typeof(TTargetParm));
var castParm = Expression.Convert(parm, typeof(TParm));
var body = ReplaceExpression(input.Body, input.Parameters[0], castParm);
body = Expression.Convert(body, typeof(TTargetReturn));
return Expression.Lambda<Func<TTargetParm, TTargetReturn>>(body, parm);
}
Sample usage:
Expression<Func<Customer, string>> expression = c => c.Name;
var convertedExpression = ConvertGeneric<Customer, string, object, object>(expression);
var backExpression = ConvertGeneric<object, object, Customer, string>(convertedExpression);

Generic Linq to Entities filter method that accepts filter criteria and properties to be filtered

I've looked into many generic linq filtering questions and their answers here in SO but none of them satisfy my needs so I thought I should create a question.
I've created many of what I call "filter provider" classes, one for each entity class in my model, to provide a simplistic search for my application. I didn't want to go into more advanced solutions like Lucene.Net because a basic filtering with matching score would suffice.
Inside each one of these provider classes there are multiple methods that will receive the filtering terms and query specific properties, returning a score for each match based on the relevance of the property. Most methods will filter multiple properties at once, but not all.
Here are two of these methods:
private IQueryable<Retailer> MatchHighRelevanceFields(string searchTerm, IQueryable<Retailer> retailers)
{
var results = retailers.Where(r =>
(r.CompanyName != null && r.CompanyName.ToUpper().Contains(searchTerm))
|| (r.TradingName != null && r.TradingName.ToUpper().Contains(searchTerm))
);
return results;
}
private IQueryable<Retailer> MatchMediumRelevanceFields(string searchTerm, IQueryable<Retailer> retailers)
{
var results = retailers.Where(r =>
(r.Address.Street != null && r.Address.Street.ToUpper().Contains(searchTerm))
|| (r.Address.Complement != null && r.Address.Complement.ToUpper().Contains(searchTerm))
);
return results;
}
These methods are replicated ad nauseum throughout each provider class and I hope I could replace them for a single method that would receive the properties to be included in the query.
Something like:
public static IQueryable<T> Match<T>(string searchTerm, IQueryable<T> data, Expression<Func<T, string>> filterProperties)
{
var results = **build the query for each property in filterProperties**
return results;
}
But I really can't figure it out. I tried using reflection but it only worked with Linq to Objects and I need a solution for Linq to Entities.
So to solve this problem we need a few puzzle pieces first. The first puzzle piece is a method that can take an expression that computes a value, and then another expression that computes a new value taking the same type the first returns, and creates a new expression that represents the result of passing the result of the first function as the parameter to the second. This allows us to Compose expressions:
public static Expression<Func<TFirstParam, TResult>>
Compose<TFirstParam, TIntermediate, TResult>(
this Expression<Func<TFirstParam, TIntermediate>> first,
Expression<Func<TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TFirstParam), "param");
var newFirst = first.Body.Replace(first.Parameters[0], param);
var newSecond = second.Body.Replace(second.Parameters[0], newFirst);
return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
This relies on the following tool to replace all instances of one expression with another:
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
We'll also need a tool to help us OR two predicate expressions together:
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var secondBody = expr2.Body.Replace(
expr2.Parameters[0], expr1.Parameters[0]);
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, secondBody), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var secondBody = expr2.Body.Replace(
expr2.Parameters[0], expr1.Parameters[0]);
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso(expr1.Body, secondBody), expr1.Parameters);
}
}
Now that we have this we can use Compose on each property selector to map it from the property results to whether or not that property value is non-null and contains the search term. We can then OR all of those predicates together to get a filter for your query:
public static IQueryable<T> Match<T>(
IQueryable<T> data,
string searchTerm,
IEnumerable<Expression<Func<T, string>>> filterProperties)
{
var predicates = filterProperties.Select(selector =>
selector.Compose(value =>
value != null && value.Contains(searchTerm)));
var filter = predicates.Aggregate(
PredicateBuilder.False<T>(),
(aggregate, next) => aggregate.Or(next));
return data.Where(filter);
}
You can do it with expression trees but it's not as simple as you might think.
public static IQueryable<T> Match<T>(this IQueryable<T> data, string searchTerm,
params Expression<Func<T, string>>[] filterProperties)
{
var parameter = Expression.Parameter(typeof (T), "source");
Expression body = null;
foreach (var prop in filterProperties)
{
// need to replace all the expressions with the one parameter (gist taken from Colin Meek blog see link on top of class)
//prop.body should be the member expression
var propValue =
prop.Body.ReplaceParameters(new Dictionary<ParameterExpression, ParameterExpression>()
{
{prop.Parameters[0], parameter}
});
// is null check
var isNull = Expression.NotEqual(propValue, Expression.Constant(null, typeof(string)));
// create a tuple so EF will parameterize the sql call
var searchTuple = Tuple.Create(searchTerm);
var matchTerm = Expression.Property(Expression.Constant(searchTuple), "Item1");
// call ToUpper
var toUpper = Expression.Call(propValue, "ToUpper", null);
// Call contains on the ToUpper
var contains = Expression.Call(toUpper, "Contains", null, matchTerm);
// And not null and contains
var and = Expression.AndAlso(isNull, contains);
// or in any additional properties
body = body == null ? and : Expression.OrElse(body, and);
}
if (body != null)
{
var where = Expression.Call(typeof (Queryable), "Where", new[] {typeof (T)}, data.Expression,
Expression.Lambda<Func<T, bool>>(body, parameter));
return data.Provider.CreateQuery<T>(where);
}
return data;
}
public static Expression ReplaceParameters(this Expression exp, IDictionary<ParameterExpression, ParameterExpression> map)
{
return new ParameterRebinder(map).Visit(exp);
}
Now you need to have a expressionvisitor to make all the expressions use one parameter
//http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx
public class ParameterRebinder : ExpressionVisitor
{
private readonly IDictionary<ParameterExpression, ParameterExpression> _map;
public ParameterRebinder(IDictionary<ParameterExpression, ParameterExpression> map)
{
_map = map;
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (_map.ContainsKey(node))
{
return _map[node];
}
return base.VisitParameter(node);
}
}
Would use it like
var matches = retailers.Match("7", r => r.Address.Street, x => x.Address.Complement).ToList();
Warning - I checked this with linq to objects using the AsQueryable but didn't run it against EF.
You can use Linq.Dynamic to build the query.
public static IQueryable<T> Match<T>(
string searchTerm,
IQueryable<T> data,
params Expression<Func<T, string>>[] filterProperties) where T : class
{
var predicates = new List<string>();
foreach (var prop in filterProperties)
{
var lambda = prop.ToString();
var columnName = lambda.Substring(lambda.IndexOf('.') + 1);
var predicate = string.Format(
"({0} != null && {0}.ToUpper().Contains(#0))", columnName);
predicates.Add(predicate);
}
var filter = string.Join("||", predicates);
var results = data.Where(filter, searchTerm);
return results;
}
Usage.
var retailers = Match(
"asd", db.Retailers, r => r.CompanyName, r => r.TradingName);
var retailers = Match(
"asd", db.Retailers, r => r.Address.Street, r => r.Address.Complement);
Limitation.
The filter can only accept basic expression.
r => r.Name
r => r.PropA.Name
r => r.PropA.PropB.Name
Try to use Expressions like those all
http://www.codeproject.com/Articles/493917/Dynamic-Querying-with-LINQ-to-Entities-and-Express

How to build dynamic query with expression tree for PLINQ

I want to bild customized OrderBy for PLINQ, but I don't know how to.
For IQueryable, use can use below code:
public static class QueryableExtensions
{
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortProperty, ListSortDirection sortOrder)
{
var type = typeof(T);
var property = type.GetProperty(sortProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
var typeArguments = new Type[] { type, property.PropertyType };
var methodName = sortOrder == ListSortDirection.Ascending ? "OrderBy" : "OrderByDescending";
var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, source.Expression, Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);
}
}
But for ParallelQuery, there's no such property Provider and Expresss.
Does anybody know how to do?
public static class QueryableExtensions
{
public static ParallelQuery<T> OrderBy<T>(this ParallelQuery<T> source, string sortProperty, ListSortDirection sortOrder)
{
...
}
}
With IQueryable, you don't need Provider for this, it's enough to create the expression and then directly call OrderBy/OrderByDescending. The only problem is that OrderBy() is generic in the type of the sorting property, which you don't know (not statically).
You can work around that by invoking OrderBy() using reflection. Or you can use dynamic:
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string sortProperty, ListSortDirection sortOrder)
{
var type = typeof(T);
var property = type.GetProperty(sortProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
if (sortOrder == ListSortDirection.Ascending)
{
return Queryable.OrderBy(source, (dynamic)orderByExp);
}
else
{
return Queryable.OrderByDescending(source, (dynamic)orderByExp);
}
}
And you can use exactly the same with PLINQ:
public static ParallelQuery<T> OrderBy<T>(this ParallelQuery<T> source, string sortProperty, ListSortDirection sortOrder)
{
var type = typeof(T);
var property = type.GetProperty(sortProperty);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByFunc = Expression.Lambda(propertyAccess, parameter).Compile();
if (sortOrder == ListSortDirection.Ascending)
{
return ParallelEnumerable.OrderBy(source, (dynamic)orderByFunc);
}
else
{
return ParallelEnumerable.OrderByDescending(source, (dynamic)orderByFunc);
}
}
Just call AsQueryable on your parallel query, and then call AsParallel when done to get back a parallel query:
public static ParallelQuery<T> OrderBy<T>(this ParallelQuery<T> source,
string sortProperty, ListSortDirection sortOrder)
{
return source.AsQueryable()
.OrderBy(sortProperty, sortOrder)
.AsParallel();
}

Categories

Resources