I currently have the following code which allows me to call any method required on the EmailAddress property of my object, and it works great:
public static Expression<Func<T, bool>> BuildEmailAddressLambda(string method, params object[] args) {
var e = Expression.Parameter(typeof(T), "e");
var propertyInfo = typeof(T).GetProperty("EmailAddress");
var m = Expression.MakeMemberAccess(e, propertyInfo);
var mi = m.Type.GetMethod(method, args.Select(a => a.GetType()).ToArray());
var c = args.Select(a => Expression.Constant(a, a.GetType())).ToArray();
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(Expression.Call(m, mi, c), e);
return lambda;
}
// called:
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("StartsWith", "r", StringComparison.OrdinalIgnoreCase);
However, I now need to amend the code so that it works for members of my object other than EmailAddress. Specifically, I would like to create a tree to cover the following expression, where multiple method calls are used:
e.GetStringValue(12).StartsWith("r", StringComparison.OrdinalIgnoreCase);
I have made several attempts, all of which end up in various errors. I feel I am missing something in the logic of how ExpressionTrees are created and some help with this would be greatly appreciated.
Thanks
Is this good for you?
public static Expression<Func<T, bool>> BuildEmailAddressLambda<T>(
string member, IEnumerable<object> memberArgs, string method, params object[] args)
{
var e = Expression.Parameter(typeof(T), "e");
var memberInfo =
(MemberInfo) typeof(T).GetField(member) ??
(MemberInfo) typeof(T).GetProperty(member) ??
(MemberInfo) typeof(T).GetMethod(member, (memberArgs ?? Enumerable.Empty<object>()).Select(p => p.GetType()).ToArray());
Expression m;
if (memberInfo.MemberType == MemberTypes.Method)
{
var a = memberArgs.Select(p => Expression.Constant(p));
m = Expression.Call(e, (MethodInfo) memberInfo, a);
}
else
{
m = Expression.MakeMemberAccess(e, memberInfo);
}
var mi = m.Type.GetMethod(method, args.Select(a => a.GetType()).ToArray());
var c = args.Select(a => Expression.Constant(a, a.GetType()));
return Expression.Lambda<Func<T, bool>>(Expression.Call(m, mi, c), e);
}
// called:
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("EmailAddress", null, "StartsWith", "r", StringComparison.OrdinalIgnoreCase);
// or
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("GetStringValue", new object[] { 12 }, "StartsWith", "r", StringComparison.OrdinalIgnoreCase);
Related
I am trying to build following query
_dbContext.Models.Where(m => m.Children.Any(c => c.Name.Contains("Foobar")))
using expression trees. I am not very familar with expression trees yet, quite frankly I find them challenging. What I've tried:
var toCompare = "Foobar";
var param = Expression.Parameter(typeof(T));
// Properties
var propertyLeft = Expression.Property(param, "Children");
// Trying to retrieve Name property of the Child type
var propertyRight = Expression.Property(Expression.Parameter(propertyLeft.Type.GetGenericArguments().Single()), "Name");
// Methods
var contains = typeof(string).GetMethod("Contains", new[] { typeof(string) })!;
var any = typeof(Queryable).GetMethods()
.Where(m => m.Name == "Any")
.First(m => m.GetParameters().Length == 2)
.MakeGenericMethod(typeof(string));
// Build c => c.Name.Contains(toCompare)
var expContains = Expression.Call(propertyRight, contains, Expression.Constant(toCompare, propertyRight.Type));
var expContainsLambda = Expression.Lambda<Func<T, bool>>(expContains, parameterExpression);
// Build m => m.Children.Any(...)
var expAny = Expression.Call(any, Expression.Constant(toCompare), expContainsLambda);
var expAnyLambda = Expression.Lambda<Func<Expression<Func<T, string>>, bool>>(expAny, parameterExpression);
// Build Models.Where(m => ...)
var expWhere = Expression.Lambda<Func<T, bool>>(expAnyLambda, param);
I am getting following error at expAny
System.ArgumentException: Expression of type 'System.String' cannot be used for parameter of type 'System.Linq.IQueryable`1[System.String]' of method 'Boolean Any[String](System.Linq.IQueryable`1[System.String], System.Linq.Expressions.Expression`1[System.Func`2[System.String,System.Boolean]])' (Parameter 'arg0')
Any ideas why? What changes are necessary to make it work?
Try the following:
var toCompare = "Foobar";
var param = Expression.Parameter(typeof(T), "m");
// m.Children
var propertyLeft = Expression.Property(param, "Children");
var childType = propertyLeft.Type.GetGenericArguments()[0];
var childParam = Expression.Parameter(childType, "c");
// c.Name
var propertyRight = Expression.Property(childParam, "Name");
// c => c.Name.Contains("Foobar")
var anyPredicate =
Expression.Lambda(
Expression.Call(propertyRight, nameof(string.Contains), Type.EmptyTypes, Expression.Constant(toCompare)),
childParam
)
// m.Children.Any(c => c.Name.Contains("Foobar"))
var anyCall = Expression.Call(typeof(Enumerable), nameof(Enumerable.Any), new [] { childType }, propertyLeft, anyPredicate);
// m => m.Children.Any(c => c.Name.Contains("Foobar"))
var expWhere = Expression.Lambda<Func<T, bool>>(anyCall, param);
(Disclaimer: I am the author of the library in question.)
I've written a library that produces various string representations from expression trees, such as C# or VB code, or Dynamic LINQ. One of the representations is the factory methods needed to create an expression tree. For example, this:
Expression<Func<Model, bool>> expr = m => m.Children.Any(c => c.Name.Contains("Foobar"));
Console.WriteLine(
expr.ToString("Factory methods", "C#")
);
produces this (using simulated types Model and ModelChild:
// using static System.Linq.Expressions.Expression
var c = Parameter(
typeof(ModelChild),
"c"
);
var m = Parameter(
typeof(Model),
"m"
);
Lambda(
Call(
typeof(Queryable).GetMethod("Any", new[] { typeof(IQueryable<ModelChild>), typeof(Expression<Func<ModelChild, bool>>) }),
MakeMemberAccess(m,
typeof(Model).GetProperty("Children")
),
Quote(
Lambda(
Call(
MakeMemberAccess(c,
typeof(ModelChild).GetProperty("Name")
),
typeof(string).GetMethod("Contains", new[] { typeof(string) }),
Constant("Foobar")
),
c
)
)
),
m
)
which you can then use to customize for your own needs.
Using Net 4.5.1, C#....
With IQueryable I have a GroupBy clause that returns a Dictionary. This is done by the following code:
public static Expression<Func<ChartJoin, Dictionary<string, object>>> GetGroupByDictionary(NameValueCollection fields)
{
var parameter = Expression.Parameter(typeof(ChartJoin));
var addMethod = typeof(Dictionary<string, object>)
.GetMethod(
"Add",
new[] { typeof(string), typeof(object) }
);
var selector = Expression.ListInit(
Expression.New(typeof(Dictionary<string, object>)),
fields.AllKeys.Select(
key => Expression.ElementInit(
addMethod,
Expression.Constant(key),
Expression.Convert(
Chart.getNestedPropertyOrField(parameter, fields[key]), // basically drills down to a nested property (note: static method not shown to save space)
typeof(object)
)
)
)
);
var lambda = Expression.Lambda<Func<ChartJoin, Dictionary<string, object>>>(selector, parameter);
return lambda;
}
The call is then:
NameValueCollection fields = new NameValueCollection();
fields.Add("Year", "Respondent.currentVisitYear");
fields.Add("Month", "Respondent.currentVisitMonth");
// .... could be more fields
<some IQueryable<ChartJoin>
.GroupBy(
Chart.GetGroupByDictionary(fields).Compile(),
new DictionaryComparer<string, object>()
);
The DictionaryComparer allows for uniqueness providing an Equals and GetHashCode implementation. I want to return a Dictionary with the Select clause. Trying just a simple example of selecting one of the GroupBy keys (for example Select(GetKey("Year").Compile())):
private static Expression<Func<IGrouping<IDictionary<string, object>, ChartJoin>, Dictionary<string, object>>> GetKey(String key)
{
var block = ? /// Need logic to get a the IGrouping.Key property and pull the value
var addMethod = typeof(Dictionary<string, object>)
.GetMethod(
"Add",
new[] { typeof(string), typeof(object) }
);
var selector = Expression.ListInit(
Expression.New(typeof(Dictionary<string, object>)),
Expression.ElementInit(
addMethod,
Expression.Constant(key),
Expression.Convert(
block,
typeof(object)
)
)
);
var lambda = Expression.Lambda<Func<IGrouping<IDictionary<string, object>, ChartJoin>, Dictionary<string, object>>>(selector, parameter);
return lambda;
}
Would be great if somebody could get me started with the above (i.e. how to create the block expression to pull a Dictionary value from a GroupBy.Key).
Answering my own question since comments is too shallow a space for code.
Basically, writing expressions for Select wasn't rocket science after a bit of experimentation. For example, here is the code for pulling a group Key into the selection:
private static Expression<Func<IGrouping<IDictionary<string, object>, Respondent>, IDictionary<string, object>>> GetKey(String field)
{
// x =>
var ParameterType = typeof(IGrouping<IDictionary<string, object>, Respondent>);
var parameter = Expression.Parameter(ParameterType, "x");
// x => x.Key
var Key = Expression.Property(parameter, "Key");
// x => x.Key[field]
Expression KeyExpression = Expression.Property(Key, "Item", new Expression[] { Expression.Constant(field) });
ParameterExpression KeyResult = Expression.Parameter(typeof(object));
BlockExpression block = Expression.Block(
new[] { KeyResult },
Expression.Assign(KeyResult, KeyExpression),
KeyResult
);
.... // <- the block is put in the selector (as shown above)
}
Doing a Count on the GroupBy was also straightforward when I got my head around static method calls:
// x => x.Count()
MethodInfo CountMethod = (typeof(Enumerable))
.GetMethods()
.First(
method => method.Name == "Count"
&& method.IsGenericMethod
)
.MakeGenericMethod(typeof(Respondent));
Expression CountExpression = Expression.Call(null, CountMethod, parameter);
Sum was also fun:
// x => x.Sum(m => m.MWEIGHT) where MWEIGHT is a ?decimal (i.e. nullable)
var m = Expression.Parameter(typeof(Respondent), "m");
PropertyInfo SumPropertyInfo = typeof(Respondent).GetProperty("MWEIGHT");
Expression SumSelector = Expression.Lambda(
Expression.Convert(Expression.MakeMemberAccess(m, SumPropertyInfo), typeof(decimal)),
m
);
MethodInfo SumMethod = (typeof(Enumerable))
.GetMethods()
.First(
method => method.Name == "Sum"
&& method.ReturnType == typeof(decimal)
&& method.IsGenericMethod
)
.MakeGenericMethod(typeof(Respondent));
Expression SumExpression = Expression.Call(null, SumMethod, parameter, SumSelector);
I try to make my custom orderby extension method, i successfully worked my code but in addition i want to list null or empty or zero values last in result, anyone can help me about that issue ?
Here is my extension method to orderby
public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)
{
//var nullExpr = Expression.Constant(null, typeof(T));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = isAsc ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
Thanks in advance
The simplest way is to use
OrderBy(e => String.IsNullOrEmpty(e.TeamName)
This doesn't require any extension method or custom IComparer implementation etc.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => String.IsNullOrEmpty(e.TeamName))
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
Without using an extension method....
Create a custom IComparer<string> to check the empty values before using the default String.Compare. The first checks will return -1 instead of 1 or 1 instead of -1, if using the standard string comparison.
/// <summary>
/// Returns -1 instead of 1 if y is IsNullOrEmpty when x is Not.
/// </summary>
public class EmptyStringsAreLast : IComparer<string>
{
public int Compare(string x, string y)
{
if (String.IsNullOrEmpty(y) && !String.IsNullOrEmpty(x))
{
return -1;
}
else if (!String.IsNullOrEmpty(y) && String.IsNullOrEmpty(x))
{
return 1;
}
else
{
return String.Compare(x, y);
}
}
}
Pass your EmptyStringsAreLast comparer into the OrderBy of Lambda expression. In this solution teams who have entered the race should appear alphabetical order, but the unaffiliated race entries should appear at then end.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => e.TeamName, new EmptyStringsAreLast())
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
This answer is perhaps what you were originally looking for - using your generic extension method:
public static IQueryable<T> OrderByFieldNullsLast<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
//We are rebuilding .OrderByDescending(p => p.SortField.HasValue).ThenBy(p => p.SortField)
//i.e. sort first by whether sortfield has a value, then by sortfield asc or sortfield desc
//create the expression tree that represents the generic parameter to the predicate
var param = Expression.Parameter(typeof(T), "p");
//create an expression tree that represents the expression p=>p.SortField.HasValue
var prop = Expression.Property(param, SortField);
var hasValue = Expression.Property(prop, "HasValue");
var exp = Expression.Lambda(hasValue, param);
string method = "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var orderByCallExpression = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
//now do the ThenBy bit,sending in the above expression to the Expression.Call
exp = Expression.Lambda(prop, param);
types = new Type[] { q.ElementType, exp.Body.Type };
method = Ascending ? "ThenBy" : "ThenByDescending";
var ThenByCallExpression = Expression.Call(typeof(Queryable), method, types,orderByCallExpression, exp);
return q.Provider.CreateQuery<T>(ThenByCallExpression);
}
Building on Dave Anson's answer, you can user Comparer.Create() to create the Comparer from a lambda. Here's an example that sorts unsorted by its myString string fields, with null or empty strings appearing last.
var sorted = unsorted.OrderBy(x => x.myString, Comparer<string>.Create((x, y) => {
if ( string.IsNullOrEmpty(y) && !string.IsNullOrEmpty(x)) return -1;
else if (!string.IsNullOrEmpty(y) && string.IsNullOrEmpty(x)) return +1;
else return string.Compare(x, y);
}))
(To put them first, switch the signs on the 1 constants)
it works for me:
private static IQueryable<T> GetOrderQuery<T>(this IQueryable<T> q, BaseFilterCollection filter)
{
q = q.OrderBy(GetExpression<T>(filter.SortField));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, filter.SortField);
var exp = Expression.Lambda(prop, param);
string method = filter.SortDirection == SortDirectionType.Asc ? "ThenBy" : "ThenByDescending";
Type[] types = { q.ElementType, exp.Body.Type };
var rs = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(rs);
}
private static Expression<Func<T, bool>> GetExpression<T>(string sortField)
{
ParameterExpression param = Expression.Parameter(typeof(T), "p");
Expression prop = Expression.Property(param, sortField);
var info = typeof(T).GetProperty(sortField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
Expression exp = Expression.Equal(prop, info.PropertyType.IsValueType
? Expression.Constant(Activator.CreateInstance(info.PropertyType))
: Expression.Constant(null));
return Expression.Lambda<Func<T, bool>>(exp, param);
}
You dont need to complicate, the easiest way is to do something like this:
YourList.OrderByDescending(x => string.IsNullOrEmpty(x.value)
Use OrderByDescending or OrderBy depending on if you want to see empty strings in the beginning or last.
Regards
I have a LINQ query which is composed by an anonymous object.
At a given point, I want to limit the results by incoming search parameters, but this can be one or more parameters, and I want to perform a "LIKE x OR LIKE y OR LIKE z" using those.
In code, it would look like this:
reservations = reservations.Where(r =>
r.GuestLastName.Contains(parameter1) || r.GuestFirstName.Contains(parameter1) ||
r.GuestLastName.Contains(parameter2) || r.GuestFirstName.Contains(parameter2) ||
// Parameter 3, 4, 5,..
);
How could I construct this dynamically, knowing that reservations is of the type IQueryable<'a> (anonymous object)? I've looked around on various resources and I can only seem to find a way to do it when I know the type, not when using anonymous types.
It's important to know that it's Linq to SQL, so it should be translated to an SQL query and not be filtered in memory...
There are two possible ways:
Building an Expression, as pointed out by Coincoin
Putting all your parameters into an array and using Any:
var parameters = new [] { parameter1, parameter2, /*...*/ }
reservations = reservations
.Where(r =>
parameters.Any(p => r.GuestFirstName.Contains(p)
|| r.GuestLastName.Contains(p)));
I would write my own generic extension method:
public static class CollectionHelper
{
public static IQueryable Filter<T>(this IQueryable source, string[] properties, string[] values)
{
var lambda = CombineLambdas<T>(properties, values);
var result = typeof (Queryable).GetMethods().First(
method => method.Name == "Where"
&& method.IsGenericMethodDefinition)
.MakeGenericMethod(typeof (T))
.Invoke(null, new object[] {source, lambda});
return (IQueryable<T>) result;
}
// combine lambda expressions using OR operator
private static LambdaExpression CombineLambdas<T>(string[] properties, string[] values)
{
var param = Expression.Parameter(typeof (T));
LambdaExpression prev = null;
foreach (var value in values)
{
foreach (var property in properties)
{
LambdaExpression current = GetContainsExpression<T>(property, value);
if (prev != null)
{
Expression body = Expression.Or(Expression.Invoke(prev, param),
Expression.Invoke(current, param));
prev = Expression.Lambda(body, param);
}
prev = prev ?? current;
}
}
return prev;
}
// construct expression tree to represent String.Contains
private static Expression<Func<T, bool>> GetContainsExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof (T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
var method = typeof (string).GetMethod("Contains", new[] {typeof (string)});
var someValue = Expression.Constant(propertyValue, typeof (string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
}
}
and the usage:
var reservations = new List<TheType>() // sample collection
{
new TheType {FirstName = "aa", LastName = "bb"},
new TheType {FirstName = "cc", LastName = "dd"},
new TheType {FirstName = "ee", LastName = "ff"}
}.AsQueryable();
var filtered = reservations
.Filter<TheType>(new[] {"FirstName", "LastName"}, new[] {"d", "e"});
/* returnes 2 elements:
* {FirstName = "cc", LastName = "dd"} and {FirstName = "ee", LastName = "ff"} */
I don't know a general solution you'd like to have - if exists any, but I hope it can be acceptable alternative which solves your case by building desired filter dynamically.
I found the solution after some debugging, but I create a WhereFilter with multiple selectors, one for FirstName and one for LastName..
This is the extension method:
public static IQueryable<T> WhereFilter<T>(this IQueryable<T> source, string[] possibleValues, params Expression<Func<T, string>>[] selectors)
{
List<Expression> expressions = new List<Expression>();
var param = Expression.Parameter(typeof(T), "p");
var bodies = new List<MemberExpression>();
foreach (var s in selectors)
{
bodies.Add(Expression.Property(param, ((MemberExpression)s.Body).Member.Name));
}
foreach (var v in possibleValues)
{
foreach(var b in bodies) {
expressions.Add(Expression.Call(b, "Contains", null, Expression.Constant(v)));
}
}
var finalExpression = expressions.Aggregate((accumulate, equal) => Expression.Or(accumulate, equal));
return source.Where(Expression.Lambda<Func<T, bool>>(finalExpression, param));
}
It can be used like this:
reservations = reservations.WhereFilter(
array_of_allowed_values,
r => r.GuestFirstName,
r => r.GuestLastName
);
I checked the trace string of the query and it actually translated to SQL, so the filtering is performed at the database.
I have a method to Generate Expression By Clause as below:
internal static Expression<Func<TModel, T>> GenExpressionByClause<TModel, T>(string column)
{
PropertyInfo columnPropInfo = typeof(TModel).GetProperty(column);
var entityParam = Expression.Parameter(typeof(TModel), "e"); // {e}
var columnExpr = Expression.MakeMemberAccess(entityParam, columnPropInfo); // {e.column}
var lambda = Expression.Lambda(columnExpr, entityParam) as Expression<Func<TModel, T>>; // {e => e.column}
return lambda;
}
So I could build lambda expression as e=>e.column.
but I want to change it to e=>string.Format("{0}",e.column). How could I refactor the code?
This should do the work:
internal static Expression<Func<TModel, T>> GenExpressionByClause<TModel, T>(string column)
{
var columnPropInfo = typeof(TModel).GetProperty(column);
var formatMethod = typeof (string).GetMethod("Format", new[] {typeof (string), typeof (object)});
var entityParam = Expression.Parameter(typeof(TModel), "e");
var columnExpr = Expression.MakeMemberAccess(entityParam, columnPropInfo);
var formatCall = Expression.Call( formatMethod, Expression.Constant("{0}"), columnExpr);
var lambda = Expression.Lambda(formatCall , entityParam) as Expression<Func<TModel, T>>;
return lambda;
}
Note that you could cache the Format MethodInfo in a static field.
Final solution:
internal static Expression<Func<TModel, string>> GenExpressionByClauseEx<TModel>(string column)
{
var columnPropInfo = typeof(TModel).GetProperty(column);
var formatMethod = typeof(String).GetMethod("Format", new[] { typeof(string), typeof(Object) });
//string.Format(
var entityParam = Expression.Parameter(typeof(TModel), "e");
var columnExpr = Expression.MakeMemberAccess(entityParam, columnPropInfo);
var columnExprObj=Expression.Convert(columnExpr, typeof(object));
var formatCall = Expression.Call(formatMethod, Expression.Constant("111--{0}"), columnExprObj);
var lambda = Expression.Lambda(formatCall, entityParam) as Expression<Func<TModel, string>>;
return lambda;
}