Dynamic Lambda Expression call - c#

I'm getting this exception when I run this code.
ParameterExpression of type System.Int64 cannot be used for delegate parameter of type System.Object
I know it's something to do with the Expression.Lambda<func<object,bool>> part of the code. Overall, I want to pass any type of ParameterExpression into this method and it will call the expression.
public static IQueryable<T> OrderData<T>(IQueryable<T> data)
{
try
{
Order order = Order.ASC;
var result = Enum.TryParse<Order>(_gridSettings.SortOrder, true, out order);
if (_gridSettings.IsSearch)
{
data = ExpressionSort(order, data, typeof(T).GetProperty(_gridSettings.SortColumn));
}
else
{
data = ExpressionSort(order, data, _defaultColumn);
}
}
catch (Exception ex)
{
log.WriteLog(MethodBase.GetCurrentMethod(), LogLevel.FATAL, ex);
}
return data;
}
private static IQueryable<T> ExpressionSort<T>(Order order, IQueryable<T> data, PropertyInfo property)
{
// Compose the expression tree that represents the parameter to the predicate.
ParameterExpression paramExpression = Expression.Parameter(property.PropertyType, property.Name);
IQueryable<T> queryableData = data.AsQueryable<T>();
switch (order)
{
case Order.ASC:
return ExecuteCall(paramExpression, paramExpression, queryableData, "OrderBy");
case Order.DESC:
return ExecuteCall(paramExpression, paramExpression, queryableData, "OrderByDescending");
}
return data;
}
private static IQueryable<T> ExecuteCall<T>(Expression expression, ParameterExpression paramExpression, IQueryable<T> queryableData, string linqMethod)
{
MethodCallExpression callExpression = Expression.Call(
typeof(Queryable),
linqMethod,
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<object, bool>>(expression, new ParameterExpression[] { paramExpression }));
// Create an executable query from the expression tree.
return queryableData.Provider.CreateQuery<T>(callExpression);
}
EDIT:
I did see this answer to a similar question
Expression of type 'System.Int32' cannot be used for return type 'System.Object'
I do not know how to apply it to my code though
EDIT 2:
The main issue is that thisExpression.Lambda<Func<object, bool>>(conversion, new ParameterExpression[] { paramExpression })); line is giving me an exception. paramExpression contains an Int64 but its expectinng an object. I dont know how to dynamically tell the Func from the information I already have or if that is possible.
GOAL:
I am trying to do something like this data.OrderBy(x=>x.DynamicProperty);

This is what you asked for, I think... I've tested it and it seems to work.
// Caching of the reflection
private static readonly MethodInfo orderByMethod = GetOrderByMethod("OrderBy");
private static readonly MethodInfo orderByDescendingMethod = GetOrderByMethod("OrderByDescending");
private static IOrderedQueryable<TSource> ExpressionSort<TSource>(Order order, IQueryable<TSource> source, PropertyInfo property)
{
// Compose the expression tree that represents the parameter to
// the predicate.
// The expression you would use is source => source.Property,
// The parameter of the lambda, source
ParameterExpression sourceExpression = Expression.Parameter(typeof(TSource), "source");
// Accessing the expression
MemberExpression propertyExpression = Expression.Property(sourceExpression, property);
// The full lambda expression. We don't need the
// Expression.Lambda<>, but still the keySelector will be an
// Expression<Func<,>>, because Expression.Lambda does it
// authomatically. LambdaExpression is simply a superclass of
// all the Expression<Delegate>
LambdaExpression keySelector = Expression.Lambda(propertyExpression, sourceExpression);
// The OrderBy method we will be using, that we have cached
// in some static fields
MethodInfo method = order == Order.ASC ? orderByMethod : orderByDescendingMethod;
// Adapted from Queryable.OrderBy (retrieved from the reference
// source code), simply changed the way the OrderBy method is
// retrieved to "method"
return (IOrderedQueryable<TSource>)source.Provider.CreateQuery<TSource>(Expression.Call(null, method.MakeGenericMethod(new Type[]
{
typeof(TSource),
property.PropertyType
}), new Expression[]
{
source.Expression,
Expression.Quote(keySelector)
}));
}
private static MethodInfo GetOrderByMethod(string methodName)
{
// Here I'm taking the long and more correct way to find OrderBy/
// OrderByDescending: looking for a public static method with the
// right name, with two generic arguments and that has the
// parameters related to those two generic arguments in a certain
// way (they must be IQueryable<arg0> and Expression<Func<arg0,
// arg1>>
MethodInfo orderByMethod = (from x in typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public)
where x.Name == methodName
let generics = x.GetGenericArguments()
where generics.Length == 2
let parameters = x.GetParameters()
where parameters.Length == 2 &&
parameters[0].ParameterType == typeof(IQueryable<>).MakeGenericType(generics[0]) &&
parameters[1].ParameterType == typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(generics))
select x).Single();
return orderByMethod;
}
Please don't ever use AsQueryable<>(). It doesn't do what you think, and it is totally useless outside unit testing and very specific use cases.

You could use my OrderByString extension. https://www.nuget.org/packages/OrderByString/ It takes strings for sort parameters. The sort parameters strings can be comma-delimited lists of property names, such as "Prop1,Prop2" or it can include a sort order as in "Prop1 DESC, Prop2 ASC".
using OrderByExtensions;
public static IQueryable<T> OrderData<T>(IQueryable<T> data)
{
try
{
Order order = Order.ASC;
var result = Enum.TryParse<Order>(_gridSettings.SortOrder, true, out order);
var sortColumn = _gridSettings.IsSearch ? _gridSettings.SortColumn : _defaultColumn;
data = data.OrderBy(sortColumn + " " + _gridSettings.SortOrder.ToString());
}
catch (Exception ex)
{
log.WriteLog(MethodBase.GetCurrentMethod(), LogLevel.FATAL, ex);
}
return data;
}
OR
You could use the following GetExpressionForProperty method that returns the expected sort expression for OrderBy, OrderByDescending, ThenBy, or ThenByDescending.
private static IQueryable<T> ExpressionSort<T>(Order order, IQueryable<T> data, PropertyInfo property)
{
Expression<Func<T, object>> propertyExpression = GetExpressionForProperty<T>(property);
return order == Order.DESC ? data.OrderByDescending(propertyExpression) : data.OrderBy(propertyExpression);
}
static Expression<Func<TSource, object>> GetExpressionForProperty<TSource>(PropertyInfo propertyInfo)
{
var param = Expression.Parameter(typeof(TSource));
return Expression.Lambda<Func<TSource, object>>(
Expression.Convert(
Expression.Property(param, propertyInfo),
typeof(object)
)
, param);
}

Try using Expression.Convert. Here's a similar question that may give you some more guidance:
Expression of type 'System.Int32' cannot be used for return type 'System.Object'

Related

Build a Generic Expression Tree .NET Core

Hello Community i am aware of this might be a possible duplicate.
How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>?
https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/
How to create a Expression.Lambda when a type is not known until runtime?
Creating expression tree for accessing a Generic type's property
There are obviously too many resources.
I am still confused though.
Could someone provide a clearer picture of what is happening in the below code.
Below i have provided some comments to help my understanding.
private Expression<Func<T, bool>> ParseParametersToFilter<T>(string parameters)
{
Expression<Func<T, bool>> finalExpression = Expression.Constant(true); //Casting error
if (string.IsNullOrEmpty(parameters))
return finalExpression;
string[] paramArray = parameters.Split(","); //parameters is one string splitted with commas
ParameterExpression argParam = Expression.Parameter(typeof(T), "viewModel"); //Expression Tree
foreach (var param in paramArray)
{
var parsedParameter = ParseParameter(param);
if (parsedParameter.operation == Operation.None)
continue; // this means we parsed incorrectly we TODO: Better way for error handling
//Property might be containment property e.g T.TClass.PropName
Expression nameProperty = Expression.Property(argParam, parsedParameter.propertyName);
//Value to filter against
var value = Expression.Constant(parsedParameter.value);
Expression comparison;
switch (parsedParameter.operation)
{ //Enum
case Operation.Equals:
comparison = Expression.Equal(nameProperty, value);
break;
//goes on for NotEquals, GreaterThan etc
}
finalExpression = Expression.Lambda(comparison, argParam);// Casting error
}
return finalExpression;
}
The above obviously is not working.
This is returned to linq query like this IEnumerable<SomeModel>.Where(ParseParametersToFilter.Compile())
I understand my mistake is a casting mistake.
How could i fix this?
After #Jeremy Lakeman answer i updated my code to look like this. Although the ViewModel i am using is quite complex. I have provided a small preview at the end.
private Expression<Func<T, bool>> ParseParametersToFilter<T>(string parameters)
{
Expression<Func<T, bool>> finalExpression = t => true;
if (string.IsNullOrEmpty(parameters))
return finalExpression;
string[] paramArray = parameters.Split(","); //parameters is one string splitted with commas
ParameterExpression argParam = Expression.Parameter(typeof(T), "viewModel"); //Expression Tree
Expression body = Expression.Constant(true);
foreach (var param in paramArray)
{
var parsedParameter = ParseParameter(param);
if (parsedParameter.operation == Operation.None)
continue; // this means we parsed incorrectly TODO: Better way for error handling
//Property might be containment property e.g T.TClass.PropName
Expression nameProperty = Expression.Property(argParam, parsedParameter.propertyName);
//Value to filter against
var value = Expression.Constant(parsedParameter.value);
switch (parsedParameter.operation)
{ //Enum
case Operation.Equals:
body = Expression.AndAlso(body, Expression.Equal(nameProperty, value));
break;
//goes on for NotEquals, GreaterThan etc
}
body = Expression.AndAlso(body, argParam);
}
return Expression.Lambda<Func<T, bool>>(body, argParam);
}
private (string propertyName, Operation operation, string value) ParseParameter(string parameter){...}
But now i get the following Exceptions
When i pass the Status as property parameter:
The binary operator Equal is not defined for the types 'model.StatusEnum' and 'System.String'.
When i pass the User.FriendlyName parameter:
Instance property 'User.FriendlyName' is not defined for type 'model.ReportViewModel'
Parameter name: propertyName
Here is how my view model looks like!
public class ReportViewModel
{
public StatusEnum Status {get;set;}
public UserViewModel User {get;set;}
}
public enum StatusEnum
{
Pending,
Completed
}
public class UserViewModel
{
public string FriendlyName {get;set;}
}
So you're trying to turn something like "a==1,b==3" into viewModel => viewModel.a == 1 && viewModel.b == 3?
I think you're already pretty close, you just need add the && (or ||), and always create a lambda;
private Expression<Func<T, bool>> ParseParametersToFilter<T>(string parameters)
{
ParameterExpression argParam = Expression.Parameter(typeof(T), "viewModel"); //Expression Tree
Expression body = Expression.Constant(true);
if (!string.IsNullOrEmpty(parameters)){
body = parameters.Split(",")
.Select(param => {
var parsedParameter = ParseParameter(param);
// ... as above, turn param into a comparison expression ...
return comparison;
})
.Aggregage((l,r) => Expression.AndAlso(l, r));
}
return Expression.Lambda<Func<T, bool>>(body, argParam);
}
And if this is for passing to entity framework, don't compile it or you'll only be able to evaluate it client side.
Here is what i came up with and works pretty well, from my tests today.
Some refactoring may be needed. I am open to suggestions.
Please make sure to check the comments inside the code.
private void ConvertValuePropertyType(Type type, string value, out dynamic converted)
{
// Here i convert the value to filter to the necessary type
// All my values come as strings.
if (type.IsEnum)
converted = Enum.Parse(type, value);
else if (type == typeof(DateTime))
converted = DateTime.Parse(value);
else if (type is object)
converted = value;
else
throw new InvalidCastException($"Value was not converted properly {nameof(value)} {nameof(type)}");
}
private MemberExpression GetContainmentMember(ParameterExpression parameterExpression, string propertyName)
{
//propertName looks like this User.FriendlyName
//So we have to first take T.User from the root type
// Then the Name property.
// I am not sure how to make this work for any depth.
var propNameArray = propertyName.Split(".");
if (propNameArray.Length > 1)
{
MemberExpression member = Expression.Property(parameterExpression, propNameArray[0]);
return Expression.PropertyOrField(member, propNameArray[1]);
}
else
{ //This needs to make sure we retrieve containment
return Expression.Property(parameterExpression, propertyName);
}
}
// ***************************************************************
// This is the core method!
private Expression<Func<T, bool>> ParseParametersToFilter<T>(string parameters)
{
Expression body = Expression.Constant(true);
ParameterExpression argParam = Expression.Parameter(typeof(T), nameof(T));
if (string.IsNullOrEmpty(parameters))
return Expression.Lambda<Func<T, bool>>(body, argParam); // return empty filter
string[] paramArray = parameters.Split(","); //parameters is one string splitted with commas
foreach (var param in paramArray)
{
var parsedParameter = ParseParameter(param);
if (parsedParameter.operation == Operation.None)
continue; // this means we parsed incorrectly, do not fail continue
//Get model
//Get property name
//Property might be containment property e.g T.TClass.PropName
//Value to filter against
MemberExpression nameProperty = GetContainmentMember(argParam, parsedParameter.propertyName);
//Convert property value according to property name
Type propertyType = GetPropertyType(typeof(T), parsedParameter.propertyName);
ConvertValuePropertyType(propertyType, parsedParameter.value, out object parsedValue);
var value = Expression.Constant(parsedValue);
switch (parsedParameter.operation)
{
//What operation did the parser retrieve
case Operation.Equals:
body = Expression.AndAlso(body, Expression.Equal(nameProperty, value));
break;
//goes on for NotEquals, GreaterThan etc
default:
break;
}
}
return Expression.Lambda<Func<T, bool>>(body, argParam);
}
private (string propertyName, Operation operation, string value) ParseParameter(string parameter){...}
This worked very good so far.

Generate sorted list using expression tree

I have a collection which is of type IQueryable, I need to sort this based on some dynamic sort fields. Sort fields are inside a list.
I write the following method to do this.
public List<T> Order<T>(IQueryable<T> source, List<string> propertyNames)
{
if(propertyNames != null && propertyNames.Count > 0)
{
var param = Expression.Parameter(typeof(T), string.Empty);
var property = Expression.PropertyOrField(param, propertyNames[0]);
var sort = Expression.Lambda(property, param);
MethodCallExpression orderByCall = Expression.Call(typeof(Queryable),"OrderBy",new[] { property.Type },Expression.Quote(sort));
if(propertyNames.Count > 1)
{
foreach(var item in propertyNames)
{
param = Expression.Parameter(typeof(T), string.Empty);
property = Expression.PropertyOrField(param, item);
sort = Expression.Lambda(property, param);
orderByCall = Expression.Call(
typeof(Queryable),
"ThenBy", new[] { typeof(T), property.Type },
orderByCall,
Expression.Quote(sort));
}
}
var results = (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(orderByCall);
if(results != null)
return results.ToList();
}
return null;
}
when I executed MethodCallExpression orderByCall = Expression.Call(typeof(Queryable),"OrderBy",new[] { property.Type },Expression.Quote(sort)); I got some exception
No generic method 'OrderBy' on type 'System.Linq.Queryable' is
compatible with the supplied type arguments and arguments. No type
arguments should be provided if the method is non-generic.
Sorry, I don't have a direct solution for your error.
Here is an alternative ("kept it simple") approach to dynamically order your data.
1) Add these extension methods somewhere in your project
public static IOrderedQueryable<TSource> OrderBy<TSource, TProperty>(this IQueryable<TSource> source
, Expression<Func<TSource, TProperty>> expression, bool descending)
{
return !descending ? source.OrderBy(expression) : source.OrderByDescending(expression);
}
public static IOrderedQueryable<TSource> ThenBy<TSource, TProperty>(this IOrderedQueryable<TSource> source
, Expression<Func<TSource, TProperty>> expression, bool descending)
{
return !descending ? source.ThenBy(expression) : source.ThenByDescending(expression);
}
2) Now you can just loop your list of property names and apply the OrderBy / ThenBy on your IQueryable.
Other idea: you could adapt your method so it accepts expressions instead of property name strings.

Expression.Like in C#

eg: x=> x.Name = "g"
I have code block like this
public Expression<Func<TEntity, bool>> SearchExpression()
{
var c = new ConstantExpression[_paramList.Count];
var b = new BinaryExpression[_paramList.Count];
BinaryExpression comparisonExpression = null;
var entity = Expression.Parameter(typeof(TEntity));
for (int i = 0; i < _paramList.Count; i++)
{
var value = Convert.ChangeType(_paramList[i].Item2 /*"g"*/, _paramList[i].Item3 /*System.String*/);
c[i] = Expression.Constant(value); //"g"
// PROBLEM IS HERE
b[i] = Expression.Equal(Expression.Property(entity, _paramList[i].Item1 /*Name*/, c[i]);
// PROBLEM IS HERE
}
_paramList.Clear();
comparisonExpression = b.Aggregate(Expression.And);
return Expression.Lambda<Func<TEntity, bool>>(comparisonExpression, entity);
}
works like charm but I need Expression.Like (Like "g" not Equal "g")
Expression.Like(Expression.Property(entity, _paramList[i].Item1), c[i])
but C# expression tree does not support Like method
UPDATE :
I wrote something like this :
Expression.Call(Expression.Property(entity, _paramList[i].Item1),
typeof(String).GetMethod("Contains"), new Expression[] { c[i] });
but I need BinaryExpression not MethodCallExpression
You can make your code work by adding an equals expression over the method call, like so:
b[i] = Expression.Equal(
Expression.Call(Expression.Property(entity, _paramList[i].Item1),
typeof (String).GetMethod("Contains"),
new Expression[] {c[i]}), Expression.Constant(true));
In pseudo code this reads as:
b[i] = entity => entity.someProperty.Contains(c[i]) == true;
Which will return a binary expression for you.
This answer does not consider your array and the 'and' aggregation, but this should be considered as a separate issue.
Consider this class:
class MyEntity { string Name { get; set; } }
We want to query:
select ... from MyEntity where Name like '%query%';
The following method is a general implementation of the above query pattern:
static Expression<Func<TEntity, bool>> Like<TEntity>(string propertyName, string queryText)
{
var parameter = Expression.Parameter(typeof (TEntity), "entity");
var getter = Expression.Property(parameter, propertyName);
//ToString is not supported in Linq-To-Entities, throw an exception if the property is not a string.
if (getter.Type != typeof (string))
throw new ArgumentException("Property must be a string");
//string.Contains with string parameter.
var stringContainsMethod = typeof (string).GetMethod("Contains", new[] {typeof (string)});
var containsCall = Expression.Call(getter, stringContainsMethod,
Expression.Constant(queryText, typeof (string)));
return Expression.Lambda<Func<TEntity, bool>>(containsCall, parameter);
}
If you want to have a pattern of query% or %query you can use string.StartsWith and string.EndsWith instead of Contains.
Also, you can share the parameter across multiple calls if you adjust the signature.
The current implementation throws an exception if the data type of the property is not a string. Look at this answer https://stackoverflow.com/a/3292773/668272 for converting numbers to strings.
I've done this in a scripting language I wrote, which allows you to say things like name like 'bob%'. The trick is that you need to map it to a method call which takes the value and regular expression and call this from within the Expression.
If you take a look at the LikeEvaluator class in my Wire scripting language you'll see how I did it:
static class LikeEvaluator
{
private static readonly MethodInfo ApplyLikeMethodInfo=typeof(LikeEvaluator).GetMethod("ApplyLike");
private static readonly MethodInfo ApplyLikeNoCaseMethodInfo=typeof(LikeEvaluator).GetMethod("ApplyLikeNoCase");
public static Expression Like(CaseMode caseMode, Expression lhs, Expression pattern)
{
Expression x=null;
if(caseMode==CaseMode.Sensitive)
{
x=Expression.Call(ApplyLikeMethodInfo,lhs,pattern);
}
else
{
x=Expression.Call(ApplyLikeNoCaseMethodInfo,lhs,pattern);
}
return x;
}
public static bool ApplyLike(string text, string likePattern)
{
string pattern=PatternToRegex(likePattern);
return Regex.IsMatch(text,pattern,RegexOptions.None);
}
public static bool ApplyLikeNoCase(string text, string likePattern)
{
string pattern=PatternToRegex(likePattern);
return Regex.IsMatch(text,pattern,RegexOptions.IgnoreCase);
}
public static string PatternToRegex(string pattern)
{
pattern=Regex.Escape(pattern);
pattern=pattern.Replace("%",#".*");
pattern=string.Format("^{0}$",pattern);
return pattern;
}
}

Creating Sorting Func<IQueryable<T>, IOrderedQueryable<T>>?

I wanna create a sorting generic Func Creating Func<IQueryable<T>, IOrderedQueryable<T>> in an extension method :
public static Func<IQueryable<T>, IOrderedQueryable<T>> GetOrderByFunc<T>(this KeyValuePair<string, SortingType> keyValuePair)
{
//I expect the following result
//Func<IQueryable<T>, IOrderedQueryable<T>> orderby = q => q.OrderByDescending(c => c.Name);
//keyValuePair.Key is name of property in type of T that I should sort T by it
switch (keyValuePair.Value)
{
case SortingType.Ascending:
// Creating Ascending Sorting Func
break;
case SortingType.Descending:
// Creating Descending Sorting Func
break;
default :
break;
}
}
Could you guide me, how I can do it?
Edit:
this sorting also, contains of count of a T's navigation property.
e.g:
// keyValuePair.Key equals "User.Count"
// User is a navigation property of T
Func<IQueryable<T>, IOrderedQueryable<T>> orderby = q => q.OrderByDescending(c => c.User.Count);
Edit:
I changed GetSelector as the following, but an exception has occurred in bodyExpression.
public static Expression GetSelector<T>(string propertyName)
{
ParameterExpression parameter = Expression.Parameter(typeof(T));
if (propertyName.Contains("."))
{
propertyName = propertyName.Substring(0, propertyName.IndexOf("."));
Type navigationPropertyCollectionType = typeof(T).GetProperty(propertyName).PropertyType;
if (navigationPropertyCollectionType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
Expression countParameter = Expression.Parameter(navigationPropertyCollectionType, "c");
MemberExpression countExpression = Expression.Property(countParameter, "Count");
//Exception: Instance property 'Users(ICollection`1)' is not defined for type 'System.Int32'
var bodyExpression = Expression.Property(countExpression, propertyName, countParameter);
return Expression.Lambda(bodyExpression, parameter);
}
}
MemberExpression bodyMemberExpression = Expression.Property(parameter, typeof(T).GetProperty(propertyName));
return Expression.Lambda(bodyMemberExpression, parameter);
}
So the first thing that we'll need is a method that can get the selector expression that selects out that property value when given the property name. It will need to build the expression from scratch:
public static Tuple<Expression, Type> GetSelector<T>(IEnumerable<string> propertyNames)
{
var parameter = Expression.Parameter(typeof(T));
Expression body = parameter;
foreach (var property in propertyNames)
{
body = Expression.Property(body,
body.Type.GetProperty(property));
}
return Tuple.Create(Expression.Lambda(body, parameter) as Expression
, body.Type);
}
Note that since this results in a chain of properties the method also returns the type of the final property, as that wouldn't be a particularly easy bit of information to access from the caller's perspective.
Because we don't know the return type of the property when calling Selector so we have no choice but to leave the return type of this method typed to Expression. We can't cast it to an Expression<Func<T, Something>>. We could have it return a Expression<Func<T, object>>, and that would work for all properties that select out a reference type, but this wouldn't be able to box value types, so it would throw a runtime exception in those cases.
Now, because we don't know the exact type of that expression we can't call OrderBy or OrderByDescending directly. We need to grab those methods through reflection and use MakeGenericMethod so that they can be created using the proper type based on inspection of that property using reflection.
public static Func<IQueryable<T>, IOrderedQueryable<T>> GetOrderByFunc<T>(
this Tuple<IEnumerable<string>, SortingType> sortCriteria)
{
var selector = GetSelector<T>(sortCriteria.Item1);
Type[] argumentTypes = new[] { typeof(T), selector.Item2 };
var orderByMethod = typeof(Queryable).GetMethods()
.First(method => method.Name == "OrderBy"
&& method.GetParameters().Count() == 2)
.MakeGenericMethod(argumentTypes);
var orderByDescMethod = typeof(Queryable).GetMethods()
.First(method => method.Name == "OrderByDescending"
&& method.GetParameters().Count() == 2)
.MakeGenericMethod(argumentTypes);
if (sortCriteria.Item2 == SortingType.Descending)
return query => (IOrderedQueryable<T>)
orderByDescMethod.Invoke(null, new object[] { query, selector.Item1 });
else
return query => (IOrderedQueryable<T>)
orderByMethod.Invoke(null, new object[] { query, selector.Item1 });
}
You would need to use Expressions
public static Func<IQueryable<T>, IOrderedQueryable<T>> GetOrderByFunc<T>(this KeyValuePair<string, SortingType> keyValuePair)
{
Func<IQueryable<T>, IOrderedQueryable<T>> result;
var p1 = Expression.Parameter(typeof (T), "p1");
var prop = Expression.PropertyOrField(p1, keyValuePair.Key);
var lambada = Expression.Lambda<Func<T, object>>(prop, new ParameterExpression[] {p1});
//keyValuePair.Key is name of property in type of T that I should sort T by it
switch (keyValuePair.Value)
{
case SortingType.Ascending:
result = source => source.OrderBy(lambada);
break;
case SortingType.Descending:
result = source => source.OrderByDescending(lambada);
break;
default:
throw new NotImplementedException();
break;
}
return result;
}

Compound expression with variable comparator?

I'm writing a query framework, and trying to make it as generic as possible.
Let's say I have a query based on person, and I want the ability to filter on both the first and last names, and in both cases I want to be able to use filter conditions like StartsWith, 'EndsWith, Contains, Equals.
So now I have a method:
private Expression<Func<Person, bool>> FirstNameFilter(Comparator comparator, string compareValue) {
switch (comparator) {
case Comparator.Equal:
return p => p.FirstName == compareValue;
case Comparator.Contains:
return p => p.FirstName.Contains(compareValue);
case Comparator.StartsWith:
return p => p.FirstName.StartsWith(compareValue);
// etc.
}
}
Now, I also want to be able to build the same filter for LastName. Seems silly and wasteful to copy and paste the whole thing over again, just replacing p.FirstName with p.LastName. I also have a bunch of other string fields that I want to filter on, and I really don't want to have to rewrite this whole method for each one!
Is there some way to abstract this, maybe using LinqKit, so that I can come out with a more generic method with the following approximate signature:
Expression<Func<Person, bool>> GetFilter(Expression<Func<Person, string>> stringExpression, Comparator comparator, string compareValue) {}
such that I could, inside FirstNameFilter, invoke it like so:
return GetFilter(p => p.FirstName, comparator, compareValue);
Something like that (untested, but you have the idea) should help you to build the needed expressions :
public static class LinqQueries
{
private static MethodInfo toLowerMethod = typeof(String).GetMethod("ToLower", Type.EmptyTypes);
private static MethodInfo startsWithMethod= typeof(String).GetMethod("StartsWith", new Type[] { typeof(String) });
private static MethodInfo containsMethod = typeof(String).GetMethod("Contains", new Type[] { typeof(String) });
private static MethodInfo endsWithMethod= typeof(String).GetMethod("EndsWith", new Type[] { typeof(String) });
public static Expression<Func<T, bool>> GetFilter(Expression<Func<T, string>> expression, Comparator comparator, string compareValue) {
ParameterExpression parameterExpression = null;
var memberExpression = GetMemberExpression(expression.Body, out parameterExpression);
Expression constExp = Expression.Constant(compareValue);
switch (comparator) {
case Comparator.Contains:
memberExpression = Expression.Call(memberExpression, containsMethod,constExp);
break;
case Comparator.StartsWith:
memberExpression = Expression.Call(memberExpression, startsWithMethod, constExp);
break;
//etc.
default :
memberExpression = Expression.Equal(memberExpression, constExp);
break;
}
return Expression.Lambda<Func<T, bool>>(memberExpression, new[]{parameterExpression});
}
private static Expression GetMemberExpression(Expression expression, out ParameterExpression parameterExpression)
{
parameterExpression = null;
if (expression is MemberExpression)
{
var memberExpression = expression as MemberExpression;
while (!(memberExpression.Expression is ParameterExpression))
memberExpression = memberExpression.Expression as MemberExpression;
parameterExpression = memberExpression.Expression as ParameterExpression;
return expression as MemberExpression;
}
if (expression is MethodCallExpression)
{
var methodCallExpression = expression as MethodCallExpression;
parameterExpression = methodCallExpression.Object as ParameterExpression;
return methodCallExpression;
}
return null;
}
}

Categories

Resources