I want to pass dynamic lambda expressions to the function below, but I’m not sure how to define the .Take() or .OrderByDescending() on the expression object.
If I want to call the function below, then I want to be able to do this:
dbprovider.Query = (x => x.ConfigurationReference == "172.16.59.175")
.Take(100)
.OrderByDescending(x.Date)
FindEntities(db, dbprovider.Query)
But I can’t (this syntax is invalid). Any ideas?
public static List<T> FindEntities<T>(TrackingDataContext dataContext, System.Linq.Expressions.Expression<Func<T, bool>> find) where T : class
{
try
{
var val = dataContext.GetTable<T>().Where(find).ToList<T>();
return val;
}
catch (Exception ex)
{
throw ex;
}
}
The parameter is of type:
That means it can take a predicate (the “where” clause), and only a predicate. Thus the only bit you can pass in there is the filter:
To do what you want, you would need to add the rest of the code in
FindEntities, so that it becomes:(note also that the
Takeshould really be after theOrderByDescending)One way you could do that would be:
and call:
However, frankly I’m not sure what the point of this would be… the caller could do all of that themselves locally more conveniently, without using
FindEntitiesat all. Just:or even:
or just: