Extension method with optional generic argument - c#

I need to implement an API based on extensions methods (i.e. I have to use a static non-generic class). API should work smoothly with LINQ fluent API and mostly with IQueryable arguments. Like this:
public static class SomeExtensions
{
public static IQueryable<TEntity> SomeMethod<TEntity>(this IQueryable<TEntity> set, ... some arguments)
{
}
}
Now, suppose the method should take some arguments plus an Expression<Func<TEntity, TResult>> one:
public static IQueryable<TEntity> SomeMethod<TEntity, TResult>(
this IQueryable<TEntity> set,
...,
Expression<Func<TEntity, TResult>> orderByExpression)
{
}
I would like to pass the orderByExpression to OrderBy method of fluent API. Or do somethong else if orderByExpression == null.
Naturally, I'd like to have something like this:
public static IQueryable<TEntity> SomeMethod<TEntity, TResult>(
this IQueryable<TEntity> set,
...,
Expression<Func<TEntity, TResult>> orderByExpression = null)
{
}
...but when calling this method w/o optional argument I have to implicitly pass generic types, because compiler doesn't know the type of TResult.
I see some possible approaches, but I don't like them much.
Define two methods: one with this argument and one w/o, and call the first from the second. I don't like it because, actually, there are many such methods in the API, and I would have to define one additional method for every one of them.
Use Expression<Func<TEntity, object>> instead of Expression<Func<TEntity, TResult>> (it is currently so). I got rid of generic type, but there is a problem with simple (value) types like int: LINQ raises an exception when trying to cast System.Int32 to System.Object.
Maybe (haven't tried yet) I could use Expression<Func<TEntity, dynamic>> - but I don't think this is a good approach at all.
Any other ideas, anyone?

Option (1) is the best from the caller perspective. Remember the main goal for the API is to make the caller's life easier, so putting additional efforts on the implementation side should be worth enough.
Option (3) is not good. You don't want to enter complications introduced by dynamic types. And EF does not like dynamic expressions.
Option (2) actually is not so bad. So if it's what you use currently, you can stay on it. All you need to make EF happy is to convert the passed expression by removing the Convert introduced for value type properties. To do so, you can use the following helper method:
internal static IQueryable<T> ApplyOrderBy<T>(
this IQueryable<T> source,
Expression<Func<T, object>> orderByExpression = null)
{
if (orderByExpression == null) return source;
var body = orderByExpression.Body;
// Strip the Convert if any
if (body.NodeType == ExpressionType.Convert)
body = ((UnaryExpression)body).Operand;
// Create new selector
var keySelector = Expression.Lambda(body, orderByExpression.Parameters[0]);
// Here we cannot use the typed Queryable.OrderBy method because
// we don't know the TKey, so we compose a method call instead
var queryExpression = Expression.Call(
typeof(Queryable), "OrderBy", new[] { typeof(T), body.Type },
source.Expression, Expression.Quote(keySelector));
return source.Provider.CreateQuery<T>(queryExpression);
}
Here is a small test showing how the above works for different property types:
var input = new[]
{
new { Id = 2, Name = "B", ParentId = (int?)1 },
new { Id = 1, Name = "A", ParentId = (int?)null },
}.AsQueryable();
var output1 = input.ApplyOrderBy(e => e.Id).ToList();
var output2 = input.ApplyOrderBy(e => e.Name).ToList();
var output3 = input.ApplyOrderBy(e => e.ParentId).ToList();
Sample usage with your example:
public static IQueryable<TEntity> SomeMethod<TEntity>(
this IQueryable<TEntity> source,
...,
Expression<Func<TEntity, object>> orderByExpression = null)
{
var result = source;
result = preprocess(result);
result = result.ApplyOrderBy(orderByExpression);
result = postprocess(result);
return result;
}

The first option you specify is the obvious and the cleanest, though most maintenance-heavy way to do this.
Additionally, you could introduce another step in your fluent syntax. Like defining:
public interface ISortableQueryable<T> : IQueryable<T>
{
IQueryable<T> WithSorting<TResult>(Expression<Func<TEntity, TResult>> orderByExpression);
}
returning it:
public static ISortableQueryable<TEntity> SomeMethod<TEntity>(
this IQueryable<TEntity> #this, ...)
{ ... }
and providing implementation of this interface where regular IQueryable calls either redirect to the IQueryable instance it receives in constructor, or some logic is performed based on the fact whether the WithSorting method was called or not.

Related

How to build expression tree from string values

I've asked a specific question elsewhere, but after no response and some investigating I've got it down to something much more generic, but I'm still struggling to build an expression tree.
I'm using a third party library which does some mappings using an interface and extension methods. Those mappings are specified as an expression tree, what I want to do is build that expression tree up from string values.
The extension method signature:
public static T UpdateGraph<T>(this DbContext context, T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null, bool allowDelete = true) where T : class, new();
The interface IUpdateConfiguration is just a marker interface, but has the following extension methods:
public static class UpdateConfigurationExtensions
{
public static IUpdateConfiguration<T> OwnedCollection<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, ICollection<T2>>> expression);
public static IUpdateConfiguration<T> OwnedCollection<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, ICollection<T2>>> expression, Expression<Func<IUpdateConfiguration<T2>, object>> mapping);
public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression);
public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression, Expression<Func<IUpdateConfiguration<T2>, object>> mapping);
}
Using an example entity:
public class Person
{
public Car Car {get;set;}
public House House {get;set;}
}
So normal explicit usage is:
dbContext.UpdateGraph(person, mapping => mapping.OwnedEntity(p => p.House).OwnedEntity(p=> p.Car));
What I need to do is build up that mapping from a list of property names,
var props = {"Car","House"}
dbContext.UpdateGraph(person, buildExpressionFromStrings<Person>(props);
I've got so far:
static Expression<Func<IUpdateConfiguration<t>, object>> buildExpressionFromStrings<t>(IEnumerable<string> props)
{
foreach (var s in props)
{
var single = buildExpressionFromString(s);
somehow add this to chaining overall expression
}
}
static Expression<Func<IUpdateConfiguration<t>, object>> buildExpressionFromString<t>(string prop)
{
var ownedChildParam = Expression.Parameter(typeof(t));
var ownedChildExpression = Expression.PropertyOrField(ownedChildParam, prop);
var ownedChildLam = Expression.Lambda(ownedChildExpression, ownedChildParam);
// Up to here I think we've built the (o => o.Car) part of map => map.OwnedEntity(o => o.Car)
// So now we need to build the map=>map.OwnedEntity(ownedChildLam) part, by calling Expression.Call I believe, but here I'm getting confused.
}
In reality, the real-world code is more complex than this (needs to deal with recursion and child properties/mappings), but I think I can get that sorted once I get the expression built for one level. I've been tearing my hair out for over a day, trying to get this sorted... To give some context, I'm using entity framework, and some configuration to define aggregate roots.
Few things to mention. Here
mapping => mapping.OwnedEntity(p => p.House).OwnedEntity(p=> p.Car)
the parts of the lambda expression body are not lambda expressions, but just chained method call expressions, the first using the lambda expression parameter and the next using the previous result.
Second, the extension method are just static method call C# sugar, and in expression trees they must be "called" as static methods.
So, building a call to
public static IUpdateConfiguration<T> OwnedEntity<T, T2>(this IUpdateConfiguration<T> config, Expression<Func<T, T2>> expression);
could be like this
static Expression BuildConfigurationCall<T>(Expression config, string propertyName)
{
var parameter = Expression.Parameter(typeof(T), "it");
var property = Expression.Property(parameter, propertyName);
var selector = Expression.Lambda(property, parameter);
return Expression.Call(
typeof(UpdateConfigurationExtensions),
nameof(UpdateConfigurationExtensions.OwnedEntity),
new [] { typeof(T), property.Type },
config,
selector);
}
and the lambda expression in question would be:
static Expression<Func<IUpdateConfiguration<T>, object>> BuildConfigurationExpression<T>(IEnumerable<string> propertyNames)
{
var parameter = Expression.Parameter(typeof(IUpdateConfiguration<T>), "config");
var body = propertyNames.Aggregate((Expression)parameter,
(config, propertyName) => BuildConfigurationCall<T>(config, propertyName));
return Expression.Lambda<Func<IUpdateConfiguration<T>, object>>(body, parameter);
}
I use Object Builder for dynamic object creating with given props. And I use DynamicQueryable for string based expressions.

Returning filtered IQueryable<T> with anonymous types

I have a set of Reports which I need to perform filtering on before returning the output. I would like to perform this with a single anonymous method to avoid duplicating the same code in different repositories. I'm using Entity Framework so the model types all related to the database and inherit from a base class called ReportBase.
This is what how I currently implement the filtering, each report type has to implement this method with a different context and returning a different IQueryable type.
private IQueryable<ReviewAgreement> GetFiltered(ReportFilter filter)
{
IQueryable<ReviewAgreement> reviewAgreementQueryable = Context.ReviewAgreements.Where(p => p.ClientWorkflowId == filter.ClientWorkflowId);
if (filter.AppraisalLevelId.HasValue)
{
reviewAgreementQueryable = reviewAgreementQueryable.Where(p => p.AppraisalLevelId == filter.AppraisalLevelId.Value);
}
return reviewAgreementQueryable;
}
I've been trying to implement this anonymously so I can reuse it, as in this non functional example.
public IQueryable<T> GetFiltered(ReportFilter filter)
{
IQueryable<T> reportQueryable = Context.Set<T>();
reportQueryable = reportQueryable.Where(p => p.ClientWorkflowId == filter.ClientWorkflowId);
if (filter.AppraisalLevelId.HasValue)
{
reportQueryable = reportQueryable.Where(p => p.AppraisalLevelId == filter.AppraisalLevelId.Value);
}
return reportQueryable;
}
The issue I am having is of course that the use of Where is ambiguous, so it cannot resolve p.ClientWorkflowId.
I have tried using a Func<T, TResult> delegate to pass in the filtering options this but the Where operation seems to want to return a list.
Is there actually a method I can use to achieve the effect I want?
Declare an interface that has the two ID properties that you need to perform this operation.
Ensure that your entities implement that interface.
Add a constraint to the generic argument that it implements that interface.
Note that if your base class defines both of the properties in question then you don't need an interface, and can simply constrain the type to that base class:
public IQueryable<T> GetFiltered<T>(ReportFilter filter) where T : ReportBase
{
// body unchanged
}
If you want to go down the route of accepting parameters to represent these properties than it's also possible. The first thing is that you'll need to accept expressions, not Func objects, so that the query provider can analyze them. This means changing the function signature to:
public IQueryable<T> GetFiltered<T>(ReportFilter filter,
Expression<Func<T, int>> clientIdSelector,
Expression<Func<T, int>> appraisalIdSelector)
{
Next, to turn these selectors into predicates that compare the value to an ID that we have is a bit more involved for expressions than it is for regular delegates. What we really need here is a Compose method; for delegates it's simple enough, to compose one method with another you just invoke it with the parameter being the result of the first. With expressions this means taking the body of one expression and replacing all instance of the parameter with the body of another, and then wrapping the whole thing up in a new Lambda.
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 itself is dependent on the ability to replace all instances of one expression with another. To do that we'll need to use the following:
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);
}
}
Now that we have all of this in place we can actually compose our selectors with a comparison to the ID values of the filter:
IQueryable<T> reportQueryable = Context.Set<T>();
reportQueryable = reportQueryable
.Where(clientIdSelector.Compose(id => id == filter.ClientWorkflowId));
if (filter.AppraisalLevelId.HasValue)
{
reportQueryable = reportQueryable
.Where(clientIdSelector.Compose(id => id == filter.AppraisalLevelId.Value));
}
return reportQueryable;

Dynamically generated lookup key for IQueryable

I have the following simple cache class that I want to use to cache items frequently read from the database.
public class Cache<TKey, TElement>
where TElement : class
{
private Dictionary<TKey, TElement> cache = new Dictionary<TKey, TElement>();
private Func<TElement, TKey> dbKey;
public Cache(Func<TElement, TKey> dbKey)
{
this.dbKey = dbKey;
}
public TElement Get(TKey key, DataContext db)
{
if (cache.ContainsKey(key)) return cache[key];
// This line throws exception. See below
var value = db.GetTable<TElement>().SingleOrDefault(x => dbKey(x).Equals(key));
if (value != null) cache[key] = value;
return value;
}
}
The usage looks like this:
var db = new DataContext();
var userCache = new Cache<string, User>(u => u.Username);
userCache.Get("dave", db);
However, the indicated line throws a NotSupportedException with the message "Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL." I understand why the exception is being thrown, but I'm not sure how to resolve it.
The cache is intended to operate in front of a number of different db tables, with different columns being used for the lookup key. My thought was to pass in a lambda to specify the lookup column, as you can see in the code. This works for IEnumerable, but not IQueryable.
Is there another way to achieve this in LINQ to SQL?
You have to use Expression Tree, not delegates and lambdas.
Change your constructor parameter type and field type to Expression<Func<TElement, TKey>>:
private Expression<Func<TElement, TKey>> dbKey;
public Cache(Expression<Func<TElement, TKey>> dbKey)
{
this.dbKey = dbKey;
}
Thanks to compiler which automatically generates Expression Tree from lambda expression at compile time, you can still call it like before:
var userCache = new Cache<string, User>(u => u.Username);
Add private helper method to combine key selector expression with equality check:
private Expression<Func<TElement, bool>> GetConditionExpression(TKey key)
{
var param = Expression.Parameter(typeof(TElement));
return
Expression.Lambda<Func<TElement, bool>>(
Expression.Equal(
Expression.Invoke(
dbKey,
param
),
Expression.Constant(key)
),
param
);
}
Call that method to get proper Expression for SingleOrDefault:
var value = db.GetTable<TElement>().SingleOrDefault(GetConditionExpression(key));
I can't test it right now with real DB, but should work. If not, at least should point you into right dirrection.

Type inference in repository methods

I have a RepositoryBase class where I define basic crud methods for my Entity Framework Context. I have these two overloads of the All() method:
public virtual IQueryable<T> All<TKey>(Expression<Func<T, bool>> predicate)
{
return All().Where(predicate);
}
public virtual PagedResult<T> All<TKey>(int startRowIndex, int maximumRows,
Expression<Func<T, TKey>> orderingKey, Expression<Func<T, bool>> predicate,
bool sortDescending = false)
{
var subset = All().Where(predicate);
IEnumerable<T> result = sortDescending
? subset.OrderByDescending(orderingKey).Skip(startRowIndex).Take(maximumRows)
: subset.OrderBy(orderingKey).Skip(startRowIndex).Take(maximumRows);
//More code ommited
}
The first method always needs me to explicitly specify the entity type, but the second doesn't. Why is this?
Example, this doesn't compile:
return All(s => s.LoanApplicationId == loanApplicationId)
And instead I must call it like this:
return All<LoanApplication>(s => s.LoanApplicationId == loanApplicationId)
But this DOES compile:
return All(0,10, s => s.Name, s => s.LoanApplicationId == loanApplicationId, false)
TKey is in the parameter list of the second (via Expression<Func<T, TKey>> orderingKey) and not the first. That supplies enough for the second to successfully infer the type when you use it with your supplied arguments (s => s.Name). You don't give yourself that luxury in the first version, so the compiler forces you to fill in the details by supplying the type parameter explicitly.
And from the looks of it, you don't need TKey in the first anyway, so possibly get rid of it (unless there is more code visible than that relatively simple implementation). And I don't think it means what your sample invocation thinks it means. TKey in the second is likely string (whatever the type of s.Name is), for example.

C# - code to order by a property using the property name as a string [duplicate]

This question already has answers here:
Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>
(24 answers)
Closed 1 year ago.
What's the simplest way to code against a property in C# when I have the property name as a string? For example, I want to allow the user to order some search results by a property of their choice (using LINQ). They will choose the "order by" property in the UI - as a string value of course. Is there a way to use that string directly as a property of the linq query, without having to use conditional logic (if/else, switch) to map the strings to properties. Reflection?
Logically, this is what I'd like to do:
query = query.OrderBy(x => x."ProductId");
Update:
I did not originally specify that I'm using Linq to Entities - it appears that reflection (at least the GetProperty, GetValue approach) does not translate to L2E.
I would offer this alternative to what everyone else has posted.
System.Reflection.PropertyInfo prop = typeof(YourType).GetProperty("PropertyName");
query = query.OrderBy(x => prop.GetValue(x, null));
This avoids repeated calls to the reflection API for obtaining the property. Now the only repeated call is obtaining the value.
However
I would advocate using a PropertyDescriptor instead, as this will allow for custom TypeDescriptors to be assigned to your type, making it possible to have lightweight operations for retrieving properties and values. In the absence of a custom descriptor it will fall back to reflection anyhow.
PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(YourType)).Find("PropertyName");
query = query.OrderBy(x => prop.GetValue(x));
As for speeding it up, check out Marc Gravel's HyperDescriptor project on CodeProject. I've used this with great success; it's a life saver for high-performance data binding and dynamic property operations on business objects.
I'm a little late to the party, however, I hope this can be of some help.
The problem with using reflection is that the resulting Expression Tree will almost certainly not be supported by any Linq providers other than the internal .Net provider. This is fine for internal collections, however this will not work where the sorting is to be done at source (be that SQL, MongoDb, etc.) prior to pagination.
The code sample below provides IQueryable extention methods for OrderBy and OrderByDescending, and can be used like so:
query = query.OrderBy("ProductId");
Extension Method:
public static class IQueryableExtensions
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
{
return source.OrderBy(ToLambda<T>(propertyName));
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
{
return source.OrderByDescending(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);
}
}
Regards, Mark.
I liked the answer from #Mark Powell, but as #ShuberFu said, it gives the error LINQ to Entities only supports casting EDM primitive or enumeration types.
Removing var propAsObject = Expression.Convert(property, typeof(object)); didn't work with properties that were value types, such as integer, as it wouldn't implicitly box the int to object.
Using Ideas from Kristofer Andersson and Marc Gravell I found a way to construct the Queryable function using the property name and have it still work with Entity Framework. I also included an optional IComparer parameter. Caution: The IComparer parameter does not work with Entity Framework and should be left out if using Linq to Sql.
The following works with Entity Framework and Linq to Sql:
query = query.OrderBy("ProductId");
And #Simon Scheurer this also works:
query = query.OrderBy("ProductCategory.CategoryId");
And if you are not using Entity Framework or Linq to Sql, this works:
query = query.OrderBy("ProductCategory", comparer);
Here is the code:
public static class IQueryableExtensions
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName, IComparer<object> comparer = null)
{
return CallOrderedQueryable(query, "OrderBy", propertyName, comparer);
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string propertyName, IComparer<object> comparer = null)
{
return CallOrderedQueryable(query, "OrderByDescending", propertyName, comparer);
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> query, string propertyName, IComparer<object> comparer = null)
{
return CallOrderedQueryable(query, "ThenBy", propertyName, comparer);
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> query, string propertyName, IComparer<object> comparer = null)
{
return CallOrderedQueryable(query, "ThenByDescending", propertyName, comparer);
}
/// <summary>
/// Builds the Queryable functions using a TSource property name.
/// </summary>
public static IOrderedQueryable<T> CallOrderedQueryable<T>(this IQueryable<T> query, string methodName, string propertyName,
IComparer<object> comparer = null)
{
var param = Expression.Parameter(typeof(T), "x");
var body = propertyName.Split('.').Aggregate<string, Expression>(param, Expression.PropertyOrField);
return comparer != null
? (IOrderedQueryable<T>)query.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
methodName,
new[] { typeof(T), body.Type },
query.Expression,
Expression.Lambda(body, param),
Expression.Constant(comparer)
)
)
: (IOrderedQueryable<T>)query.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
methodName,
new[] { typeof(T), body.Type },
query.Expression,
Expression.Lambda(body, param)
)
);
}
}
Yes, I don't think there's another way than Reflection.
Example:
query = query.OrderBy(x => x.GetType().GetProperty("ProductId").GetValue(x, null));
query = query.OrderBy(x => x.GetType().GetProperty("ProductId").GetValue(x, null));
Trying to recall exact syntax off the top of my head but I think that is correct.
Warning ⚠️
You just can use Reflection in case that data is in-memory. Otherwise, you will see some error like below when you work with Linq-2-EF, Linq-2-SQL, etc.
#Florin Vîrdol's comment
LINQ to Entities does not recognize the method 'System.Object
GetValue(System.Object)' method and this method cannot be translated
into a store expression.
Why 🤔
Because when you write code to provide a query to Linq query provider. It is first translated into an SQL statement and then executed on the database server.
(See image below, from https://www.tutorialsteacher.com/linq/linq-expression)
Solution ✅
By using Expression tree, you can write a generic method like this
public static IEnumerable<T> OrderDynamic<T>(IEnumerable<T> Data, string propToOrder)
{
var param = Expression.Parameter(typeof(T));
var memberAccess = Expression.Property(param, propToOrder);
var convertedMemberAccess = Expression.Convert(memberAccess, typeof(object));
var orderPredicate = Expression.Lambda<Func<T, object>>(convertedMemberAccess, param);
return Data.AsQueryable().OrderBy(orderPredicate).ToArray();
}
And use it like this
var result = OrderDynamic<Student>(yourQuery, "StudentName"); // string property
or
var result = OrderDynamic<Student>(yourQuery, "Age"); // int property
And it's also working with in-memory by converting your data into IQueryable<TElement> in your generic method return statement like this
return Data.AsQueryable().OrderBy(orderPredicate).ToArray();
See the image below to know more in-depth.
Demo on dotnetfiddle
More productive than reflection extension to dynamic order items:
public static class DynamicExtentions
{
public static object GetPropertyDynamic<Tobj>(this Tobj self, string propertyName) where Tobj : class
{
var param = Expression.Parameter(typeof(Tobj), "value");
var getter = Expression.Property(param, propertyName);
var boxer = Expression.TypeAs(getter, typeof(object));
var getPropValue = Expression.Lambda<Func<Tobj, object>>(boxer, param).Compile();
return getPropValue(self);
}
}
Example:
var ordered = items.OrderBy(x => x.GetPropertyDynamic("ProductId"));
Also you may need to cache complied lambas(e.g. in Dictionary<>)
Reflection is the answer!
typeof(YourType).GetProperty("ProductId").GetValue(theInstance);
There's lots of things you can do to cache the reflected PropertyInfo, check for bad strings, write your query comparison function, etc., but at its heart, this is what you do.
Also Dynamic Expressions can solve this problem.
You can use string-based queries through LINQ expressions that could have been dynamically constructed at run-time.
var query = query
.Where("Category.CategoryName == #0 and Orders.Count >= #1", "Book", 10)
.OrderBy("ProductId")
.Select("new(ProductName as Name, Price)");
I think we can use a powerful tool name Expression an in this case use it as an extension method as follows:
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, bool descending)
{
var type = typeof(T);
var property = type.GetProperty(ordering);
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp =
Expression.Call(typeof(Queryable), (descending ? "OrderByDescending" : "OrderBy"),
new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(resultExp);
}

Categories