I have a generic class with a method that needs to sort a generic entity.
However, an error occurs saying that it does not recognize the Reflection GetProperty method, since lambda can not translate.
How can I do this sort ordering logic?
public IEnumerable<TEntity> GetAll()
{
var obj = _repository.GetAll()
.OrderByDescending(x => x.GetType().GetProperty(typeof(TEntity).Name + "Id"));
return obj.Pagination();
}
Here is the error image:
The error says it all.
Linq to Entities doesn't know how to translate x.GetType().GetProperty(typeof(TEntity).Name + "Id") into SQL.
You can materialize the results first, so it'll be linq to objects:
_repository.GetAll().ToList()
.OrderByDescending(x => x.GetType().GetProperty(typeof(TEntity).Name + "Id"));
You can use _repository.GetAll().Queryable().OrderByDescending(x => x.GetType().GetProperty(typeof(TEntity).Name + "Id"));
To build a LINQ query dynamically, use Expression Trees. This is how your method may look like:
public IEnumerable<TEntity> GetAll()
{
IQueryable<TEntity> obj = _repository.GetAll();
PropertyInfo keyProperty = typeof(TEntity).GetProperty(string.Concat(typeof(TEntity).Name, "Id"));
Expression parameter = Expression.Parameter(typeof(TEntity));
Expression predicate = Expression.Lambda(Expression.Property(parameter, keyProperty), parameter);
Expression queryExpression = Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { typeof(TEntity), keyProperty.PropertyType }, obj, predicate);
obj = obj.Provider.CreateQuery<TEntity>(queryExpression);
return obj.Pagination();
}
Related
In short, I'm looking to do what this guy did, but with Entity Framework 6.
Implementing the proposed solution results in the error "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities." Since the proposed solution uses Invoke, this is obviously an issue.
I understand that there's a way to harness a custom Compose method to rewrite the expression tree without using Invoke, but I can't seem to wrap my head around it.
Here's what I'm trying to write.
I build an IQueryable<TEntity> dynamically using a QueryParameters object that's just a bag of properties to use for the WHERE clauses. TEntity is a standard code-first EF entity with data annotations all over the place. The query contruction looks something like this:
IQueryable<TEntity> query = Context.Set<TEntity>();
if (queryParams == null)
return query;
if (!string.IsNullOrWhiteSpace(queryParams.FirstName))
{
if (queryParams.ExactSearch)
{
query = query.Where(x => x.FirstName == queryParams.FirstName);
}
else
{
if (queryParams.PreferStartsWith)
{
query = query.Where(
x => x.FirstName.ToLower()
.StartsWith(
queryParams.FirstName
.ToLower()));
}
else
{
query = query.Where(
x => x.FirstName.ToLower()
.Contains(
queryParams.FirstName
.ToLower()));
}
}
}
// ... repeat for all of queryParams' string props.
// DateTime, int, bool, etc have their own filters.
This gets repeated for every query parameter for a string field to be queried. Obviously, this results in a lot of repeated code. I would love to be able to write a filter with a signature like this:
public static IQueryable<TEntity> Search<TEntity>(
this IQueryable<TEntity> query,
Expression<Func<TEntity, string>> fieldExpression,
string searchValue,
bool exactSearch = true,
bool useStartsWithOverContains = false) {...}
Which I can then consume like this:
if (!string.IsNullOrWhiteSpace(queryParams.FirstName))
{
query = query.Search(
x => x.FirstName,
queryParams.FirstName,
queryParams.ExactSearch,
queryParams.PreferStartsWith);
}
The closest I've come a definition for that extension method is the below, but as mentioned, it produces that "'Invoke' is not supported in LINQ to Entities" error:
public static IQueryable<TEntity> Search<TEntity>(
this IQueryable<TEntity> query,
Expression<Func<TEntity, string>> fieldExpression,
string searchValue,
bool exactSearch = true,
bool useStartsWithOverContains = false)
{
if (string.IsNullOrWhiteSpace(searchValue))
return query;
searchValue = searchValue.Trim();
Expression<Func<TEntity, bool>> expression;
if (exactSearch)
{
var x = Expression.Parameter(typeof(TEntity), "x");
var left = Expression.Invoke(fieldExpression, x);
var right = Expression.Constant(searchValue);
var equalityExpression = Expression.Equal(left, right);
expression = Expression.Lambda<Func<TEntity, bool>>(
equalityExpression,
x);
}
else
{
searchValue = searchValue.ToLower();
var x = Expression.Parameter(typeof(TEntity), "x");
var fieldToLower = Expression.Call(
Expression.Invoke(fieldExpression, x),
typeof(string).GetMethod(
"ToLower",
Type.EmptyTypes));
var searchValueExpression =
Expression.Constant(searchValue);
var body = Expression.Call(
fieldToLower,
typeof(string).GetMethod(
useStartsWithOverContains ? "StartsWith" : "Contains",
new[] { typeof(string) }),
searchValueExpression);
expression = Expression.Lambda<Func<TEntity, bool>>(
body,
x);
}
return query.Where(expression);
}
I started to include the Compose method I mentioned, but I got lost really quickly, and thus removed it.
Open to any guidance! Thank you!
This is much easier to do by composing expressions than it is by trying to manually construct the expressions every single time. It's faster to write, so much less error prone, and actually ends up with code you can actually read at the end of it. All you need to do is write the code for how you use the value in the composed expression, which you already have from your original code.
public static IQueryable<TEntity> Search<TEntity>(
this IQueryable<TEntity> query,
Expression<Func<TEntity, string>> fieldExpression,
string searchValue,
bool exactSearch = true,
bool useStartsWithOverContains = false)
{
if (string.IsNullOrWhiteSpace(searchValue))
return query;
searchValue = searchValue.Trim();
if (exactSearch)
{
return query.Where(fieldExpression.Compose(field => field == searchValue));
}
else if (useStartsWithOverContains)
{
return query.Where(fieldExpression.Compose(field => field.StartsWith(searchValue.ToLower())));
}
else
{
return query.Where(fieldExpression.Compose(field => field.Contains(searchValue.ToLower())));
}
}
Note you should probably go with an enum for "Comparison" or something like that, rather than having two booleans. For example, right now someone can say that they don't want an exact sure but that they do want to use starts with. Just have one parameter with the three options.
I'm trying to build a filter for a MongoDB Collection in c# with Reflection.
IQueryable<Notification> collQuery = collection.AsQueryable()
.Where(entity =>
entity.GetType().GetProperty(filterProp.Name).GetValue(entity) == filter.FilterValue);
but when I call
collQuery.ToList()
I receive
{document}.GetType().GetProperty("SenderName").GetValue({document}) is not supported.
Am I doing something wrong or this approach cannot be followed?
You can not use reflection inside IQueryable expression, but you can use it to create expression manually. Use thid method:
public static Expression<Func<Notification, bool>> CreateWherExpression(
string propertyName, string filterValue)
{
var notificationType = typeof(Notification);
var entity = Expression.Parameter(notificationType, "entity");
var body = Expression.Equal(
Expression.Property(entity, propertyName),
Expression.Constant(filterValue));
return Expression.Lambda<Func<Notification, bool>>(body, entity);
}
Now it is possible to apply it like this:
var where = CreateWherExpression(filterProp.Name, filter.FilterValue);
IQueryable<Notification> collQuery = collection
.AsQueryable()
.Where(where);
Using
IQueryable<T> query - where T : BaseEntity
I have the following code, used in a generic method - which uses reflection to call the .Where() method (this is working):
var predicate = Expression.Lambda(body, item);
MethodInfo whereCall = (typeof(Queryable).GetMethods().First(mi => mi.Name == "Where" && mi.GetParameters().Length == 2).MakeGenericMethod(query.ElementType));
MethodCallExpression call = Expression.Call(whereCall, new Expression[] { query.Expression, predicate });
query = query.Provider.CreateQuery<T>(call);
I would like to use something like this (and avoid reflection):
var predicate = Expression.Lambda<Func<T, bool>>(body, item);
query = query.Where(predicate);
But the problem with this code is that T is used as the base-type, and not the derived-type at run-time.
How can I cast T as query.ElementType (the derived type) ?
Your second piece of code is indeed better than the first one. You will need to invoke the Expression.Lambda method using reflection somehow. A convenient way to do that is this:
static IQueryable<T> CreateQuery<T>(IQueryable<T> query, ...) {
var predicate = Expression.Lambda<Func<T, bool>>(body, item);
query = query.Where(predicate);
return query;
}
Invoke this method using T as the derived type. You can perform that call using MakeGenericMethod. Depending on your scenario it might be enough to say:
CreateQuery((dynamic)query);
I am using Entity Framework and am building up a IQueryable<T>
IQueryable<Message> query = db.Messages;
query = query.OrderByDescending(m => m.Created);
query = query.Where(m => m.Deleted == false);
if (lastTime != null)
query = query.Where(m => m.Created < lastTime);
var func = ExpressionHelper.GetPredicate(stream);
query = query.Where(func).AsQueryable; *** ISSUE HERE?? ***
query = query.Skip(skip)
.Take(count)
.Include(m => m.Tags)
.Include(m => m.Properties);
var results = query.ToList();
The issue is the Tags and Properties are not populated in the final list.
I believe it has something to do with the Func<> I am passing in and also believe that after it although AsQueryable is used it doesn't represent IQueryable or does no longer connect to the database.
Is there a way to get Tags and Properties populated?
I'm not sure if making the Func<> to be Expression<Func<>> would help and if so is there a way to convert the below to be Expression<Func<>>
UPDATE:
public static Func<Message, bool> GetPredicate(string expression)
{
Func<Message, bool> result = null;
try
{
ParameterExpression parameter = Expression.Parameter(typeof(Classes.Message), "Message");
var lambda = DynamicExpression.ParseLambda(new[] { parameter }, null, expression);
result = lambda.Compile() as Func<Message, bool>;
}
catch (Exception e)
{
Log.Fatal(e);
}
return result;
}
At this point here:
query.Where(func) // where func is Func<...>
you have switched into LINQ-to-Objects. Anything you do after than (in terms of .Include etc) is irrelevant - you are no longer composing an EF query. You have a thin IQueryable<T> wrapper over a LINQ-to-Objects version of the sequence as it was at that line, i.e.
query = query.Where(func).AsQueryable(); // this is just a thing veneer over L2O
Switching to Expression<Func<...>> is likely to help.
If you absolutely positively can't generate an Expression, you could move the Include etc above this point.
I haven't actually wired this in so it may be slightly off, but as Marc says you've swapped into Linq to Objects at the AsQueryable.
If you remove that call, and then switch the GetPredicate to below it should work. It's the same except that we don't need to compile the lambda, this will happen when the query reaches entity framework.
public static Expression<Func<Message, bool>> GetPredicate(string expression)
{
Expression<Func<Message, bool>> result = null;
try
{
ParameterExpression parameter = Expression.Parameter(typeof(Classes.Message), "Message");
var lambda = DynamicExpression.ParseLambda(new[] { parameter }, null, expression);
result = (Expression<Func<Message, bool>>)lambda;
}
catch (Exception e)
{
Log.Fatal(e);
}
return result;
}
I started with the IQueryable extension methods from this example on CodePlex.
What i believe i need is an IQueryable extension method to "Where", where the method signature looks like:
public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword)
and effectively does this (assuming T.columnName is of type string):
source.Where(p => p.ColumnName.Contains("keyword"))
using the above CodePlex example, i think i understand how he got the OrderBy method working, but my problem seems a bit more complex and I don't know how to get the Contains("keyword") part working.
Thanks in advance,
--Ed
Update: 9/13/2010 6:26pm PST
I thought the following would work, but end up getting a NotSupportedException (The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.) when I execute the expression via Count(). Any ideas?
public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword)
{
var type = typeof(T);
var property = type.GetProperty(columnName);
if (property.PropertyType == typeof(string))
{
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var sel = Expression.Lambda<Func<T, string>>(propertyAccess, parameter);
var compiledSel = sel.Compile();
return source.Where(item => compiledSel(item).Contains(keyword));
}
else
{
return source;
}
}
public static IQueryable<T> Where<T>(
this IQueryable<T> source, string columnName, string keyword)
{
var arg = Expression.Parameter(typeof(T), "p");
var body = Expression.Call(
Expression.Property(arg, columnName),
"Contains",
null,
Expression.Constant(keyword));
var predicate = Expression.Lambda<Func<T, bool>>(body, arg);
return source.Where(predicate);
}
Well a couple of years later, but if anybody still need this, here it is:
public static IQueryable<T> Has<T>(this IQueryable<T> source, string propertyName, string keyword)
{
if (source == null || propertyName.IsNull() || keyword.IsNull())
{
return source;
}
keyword = keyword.ToLower();
var parameter = Expression.Parameter(source.ElementType, String.Empty);
var property = Expression.Property(parameter, propertyName);
var CONTAINS_METHOD = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var TO_LOWER_METHOD = typeof(string).GetMethod("ToLower", new Type[] { });
var toLowerExpression = Expression.Call(property, TO_LOWER_METHOD);
var termConstant = Expression.Constant(keyword, typeof(string));
var containsExpression = Expression.Call(toLowerExpression, CONTAINS_METHOD, termConstant);
var predicate = Expression.Lambda<Func<T, bool>>(containsExpression, parameter);
var methodCallExpression = Expression.Call(typeof(Queryable), "Where",
new Type[] { source.ElementType },
source.Expression, Expression.Quote(predicate));
return source.Provider.CreateQuery<T>(methodCallExpression);
}
I called my method "Has" just to keep it short, sample usage:
filtered = filtered.AsQueryable().Has("City", strCity)
And you can concatenate to make it even more expressive:
filtered = filtered.AsQueryable().Has("Name", strName).Has("City", strCity).Has("State", strState);
By the way, the "IsNull()" attached to the strings is just another simple extension method:
public static Boolean IsNull(this string str)
{
return string.IsNullOrEmpty(str);
}
The .Contains("keyword") part is exactly right in your example.
It's the p.ColumnName part that's going to cause trouble.
Now, there are a number of ways of doing this, generally involving either reflection or Expression<>, neither of which is particularly efficent.
The problem here is by passing the column name as a string, you are doing to undo that exact thing LINQ was invented to allow.
However, there are probably better ways of accomplishing your overall task besides that way.
So, let's look at alternate ways:
You want to be able to say :
var selector = new Selector("Column1", "keyword");
mylist.Where(item => selector(item));
and have it was the equivalent of
mylist.Where(item=> item.Column1.Contains("keyword"));
How 'bout we go with:
Func<MyClass, string> selector = i => i.Column1;
mylist.Where(item => selector(item).Contains("keyword"));
or
Func<MyClass, bool> selector = i => i.Column1.Contains("keyword");
mylist.Where(item => selector(item));
These are easily expanded for alternatives:
Func<MyClass, string> selector;
if (option == 1)
selector = i => i.Column1;
else
selector = i => i.Column2;
mylist.Where(item => selector(item).Contains("keyword"));
See, in generics type of the object works dynamically. So when p.ColumnName is taken as string, the Contains of string is been executed.
Generally for any lambda expression you specify, it parses the thing into an Expression and ultimately produces the output at runtime.
If you see my post :
http://www.abhisheksur.com/2010/09/use-of-expression-trees-in-lamda-c.html
you will understand how it works actually.