Generic order by parameter - c#

I'm trying to do a generic method that would accept an order by parameter that I could then inspect for it's name and attributes before building the query and sending it to the database.
I was thinking of something like:
public class SearchEngine<T>
{
// Removed other parameters to make it simple
public IEnumerable<T> Search<K>(Func<T, K> orderBy)
{
throw new NotImplementedException();
}
}
I was hoping to consume it later with:
var searchEngine = new SearchEngine<User>();
var result = searchEngine.Search(x => x.Name);
My goal is to get a hold, inside of the search method, of that property so that I could get the name of the property, "Name" in this case, and even after it, use reflection to get its attributes to get other information I have setup.
I know about getting the attributes, what I'm not sure is how can I get the info of the property being passed. I'm sure Linq does this in some way, I just don't know how.
If I tried something like:
var result = searchEngine.Search<PropertyInfo>(x => x.Name);
This wouldnt work since the parameter is returning a string in this case.
Any ideas would be appreciated.

Use expression tree and change Search method parameter type to Expression<Func<T, K>>:
public IEnumerable<T> Search<K>(Expression<Func<T, K>> orderBy)
{
var memberExpression = orderBy.Body as MemberExpression;
if (memberExpression == null)
throw new ArgumentException("orderBy");
var member = memberExpression.Member;
var memberName = member.Name;
return null;
}
It will throw ArgumentException when orderBy is not simple, member expression.
You can still call the method the same way:
var result = searchEngine.Search(x => x.Name);

Change parameter type of Search to:
Expression<Func<T, K>>
and try like this:
public class SearchEngine<T>
{
// Removed other parameters to make it simple
public IEnumerable<T> Search<K>(Expression<Func<T, K>> orderBy)
{
var name = GetMemberName(orderBy.Body);
throw new NotImplementedException();
}
static string GetMemberName(Expression expression)
{
string memberName = null;
var memberExpression = expression as MemberExpression;
if (memberExpression != null)
memberName = memberExpression.Member.Name;
var unaryExpression = expression as UnaryExpression;
if (unaryExpression != null)
memberName = ((MemberExpression) unaryExpression.Operand).Member.Name;
var methodCallExpression = expression as MethodCallExpression;
if (methodCallExpression != null)
memberName = methodCallExpression.Method.Name;
Contract.Assume(memberName != null);
return memberName;
}
}

You can use Dynamic Linq. The syntax would be something like:
string criteria = "Age < 40 and Age > 30";
string sort = "Name";
var result = searchEngine.Where(criteria).OrderBy(sort);

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.

EF Core - Expression Tree Equivalent for IQueryable Search

I have an initial workflow put together that allows me to perform inclusive searches for string properties of objects contained in an IQueryable:
public static IQueryable ApplySearch(this IQueryable queryable, string search)
{
// validation omitted for brevity
var expression = queryable
.Cast<object>()
.Where(item => item.SearchStringTree(search))
.Expression;
var result = queryable.Provider.CreateQuery(expression);
return result;
}
static bool SearchStringTree<T>(this T value, string search) =>
value.GetObjectStrings().Any(s => s.Contains(search.ToLower()));
static IEnumerable<string> GetObjectStrings<T>(this T value)
{
var strings = new List<string>();
var properties = value.GetType()
.GetProperties()
.Where(x => x.CanRead);
foreach (var prop in properties)
{
var t = prop.PropertyType.ToString().ToLower();
var root = t.Split('.')[0];
if (t == "system.string")
{
strings.Add(((string)prop.GetValue(value)).ToLower());
}
else if (!(root == "system"))
{
strings.AddRange(prop.GetValue(value).GetObjectStrings());
}
}
return strings;
}
Would it be possible to apply this concept in a way that Entity Framework can translate prior to DbContext execution?
I've been looking into potentially using Expression Trees to accomplish this.
Here's a working Repl.it showing the IQueryable implementation above.
You definitely need to build expression tree, basically multi or (C# ||) predicate expression for all (nested) string properties.
Something like this (expression version of your code):
public static class FilterExpression
{
public static IQueryable<T> ApplySearch<T>(this IQueryable<T> source, string search)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (string.IsNullOrWhiteSpace(search)) return source;
var parameter = Expression.Parameter(typeof(T), "e");
// The following simulates closure to let EF Core create parameter rather than constant value (in case you use `Expresssion.Constant(search)`)
var value = Expression.Property(Expression.Constant(new { search }), nameof(search));
var body = SearchStrings(parameter, value);
if (body == null) return source;
var predicate = Expression.Lambda<Func<T, bool>>(body, parameter);
return source.Where(predicate);
}
static Expression SearchStrings(Expression target, Expression search)
{
Expression result = null;
var properties = target.Type
.GetProperties()
.Where(x => x.CanRead);
foreach (var prop in properties)
{
Expression condition = null;
var propValue = Expression.MakeMemberAccess(target, prop);
if (prop.PropertyType == typeof(string))
{
var comparand = Expression.Call(propValue, nameof(string.ToLower), Type.EmptyTypes);
condition = Expression.Call(comparand, nameof(string.Contains), Type.EmptyTypes, search);
}
else if (!prop.PropertyType.Namespace.StartsWith("System."))
{
condition = SearchStrings(propValue, search);
}
if (condition != null)
result = result == null ? condition : Expression.OrElse(result, condition);
}
return result;
}
}
The non generic version is not much different - just instead of Where extension method you need to generate a "call" to it in the query expression tree:
public static IQueryable ApplySearch(this IQueryable source, string search)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (string.IsNullOrWhiteSpace(search)) return source;
var parameter = Expression.Parameter(source.ElementType, "e");
var value = Expression.Property(Expression.Constant(new { search }), nameof(search));
var body = SearchStrings(parameter, value);
if (body == null) return source;
var predicate = Expression.Lambda(body, parameter);
var filtered = Expression.Call(
typeof(Queryable), nameof(Queryable.Where), new[] { source.ElementType },
source.Expression, Expression.Quote(predicate));
return source.Provider.CreateQuery(filtered);
}
While this works, it's not much useful because all LINQ extensions methods (including AsEnumerable(),ToList()` etc.) work with generic interface.
Also in both cases, the type of the query element must be known in advance, e.g. T in the generic version, query.ElementType in the non generic version. This is because expression tree are processed in advance, when there are no "objects", hence it can't use item.GetType(). For the same reason, IQueryable translators like EF Core don't like Cast "calls" inside the query expression tree.

How extract list values from expression

I need to read the values from one array, that is provided as an expression.
I have this method
public ExpressionAnalizer<TModel> where TModel : class
{
public string BuildExpression(Expression<Func<TModel, bool>> expression)
{
if (expression?.Body is MethodCallExpression)
return BuildMethodCallExpression(expression);
throw new ArgumentException($"The expression '{expression?.Body}' is unsupported");
}
public string BuildMethodCallExpression(Expression<Func<TModel, bool>> expression)
{
var body = expression.Body as MethodCallExpression;
//TODO: I can't find a property that has the values of IEnumerable
return null;
}
}
And is called like this
//PersonModel is a plain class with some properties.
var analizer= new ExpressionAnalizer<PersonModel>("");
var names = new List<string>() {"n1", "n2", "n3"};
//I want to get "Email contains ('n1', 'n2', 'n3')". I can read Email property, the call method name "Contains", but not itself values
var response = analizer.BuildExpression(x => names.Contains(x.Email));
Any idea? I was thinking to compile the expression, but I get stuck on "Closure" class, because System.Runtime.CompilerServices.Closure is private and I cannot use it.
By the way, I'm using .NET Core 1.0
EDIT
I need to get a string like
email contains ('n1','n2','n3')
And the input parameter need to be always an Expression
Expression<Func<TModel, bool>>
This is because internally, this method can receive any expression, like
x => x.SomeProperty == "n1"
Internally, I handle the expression.Body type and I have an implementation for different use cases.
The case I cannot figure how can I implement is when the input expression is
var someList = new List<string>() { "string1", "anotherString", "finalString" };
someObject.SomeProperty<SomeTModel>(x => someList.Contains(x.SomeProperty))
Here is the full implementation of your ExpressionAnalizer class which will give you the desired output.
public class ExpressionAnalizer<TModel> where TModel : class
{
public string BuildExpression(Expression<Func<TModel, bool>> expression)
{
if (expression?.Body is MethodCallExpression)
return BuildMethodCallExpression(expression);
throw new ArgumentException($"The expression '{expression?.Body}' is unsupported");
}
public string BuildMethodCallExpression(Expression<Func<TModel, bool>> expression)
{
var body = expression.Body as MethodCallExpression;
//Get Method Name
string method = body.Method.Name;
//Get List of String Values
var methodExpression = ResolveMemberExpression(body.Object);
var listValues = ReadValue(methodExpression);
var vString = string.Format("'{0}'", string.Join("' , '", (listValues as List<string>)));
//Read Propery Name
var argExpression = ResolveMemberExpression(body.Arguments[0]);
var propertyName = argExpression.Member.Name;
return $"{propertyName} {method} ({vString})";
}
public MemberExpression ResolveMemberExpression(Expression expression)
{
if (expression is MemberExpression) return (MemberExpression)expression;
if (expression is UnaryExpression) return (MemberExpression)((UnaryExpression)expression).Operand;
throw new NotSupportedException(expression.ToString());
}
private object ReadValue(MemberExpression expression)
{
if (expression.Expression is ConstantExpression)
{
return (((ConstantExpression)expression.Expression).Value)
.GetType()
.GetField(expression.Member.Name)
.GetValue(((ConstantExpression)expression.Expression).Value);
}
if (expression.Expression is MemberExpression) return ReadValue((MemberExpression)expression.Expression);
throw new NotSupportedException(expression.ToString());
}
}
Usage:
var analizer= new ExpressionAnalizer<PersonModel>();
var names = new List<string>() {"n1", "n2", "n3"};
var person = new PersonModel{ Email = "Email 1"};
var response = analizer.BuildExpression( x => names.Contains(x.Email));
Response:
Email Contains ('n1' , 'n2' , 'n3')

Conversion of Expression with nullable DateTime gives an error

In a class, I have a static method to fetch the PropertyInfo by supplying an expression.
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
MemberExpression me;
switch (expressie.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expressie.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expressie.Body as MemberExpression;
break;
}
if (me == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return (PropertyInfo)me.Member;
}
To make my code more type safe, I changed the expressions that I'm using in part of the methods which make a call to this function.
// Old
Expression<Func<T, Object>> expression;
// New (TProp instead of Object)
Expression<Func<T, TProp>> expression;
As a result, I can't use the old 'GetPropertyInfo' method anymore, because the expression does not match the requested parameters. So I created a new one.
public static PropertyInfo GetPropertyInfo<T, TProp>(Expression<Func<T, TProp>> expressie)
{
MemberExpression me;
switch (expressie.Body.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
var ue = expressie.Body as UnaryExpression;
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
break;
default:
me = expressie.Body as MemberExpression;
break;
}
if (me == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return (PropertyInfo)me.Member;
}
The body of this new expression is exactly the same as the existing one. Because I would like to stick with the DRY principle, I searched for a way to use one method body for both cases. This is possible by first converting the 'Expression<Func<T, TProp>>' into 'Expression<Func<T, Object>>' and passing the converted expression to the 'old' GetPropertyInfo(...) method. I am not able to completely remove the old 'GetPropertyInfo' method, because of other pieces of code still relying on it.
The conversion method that I'm using is the following:
private static Expression<Func<T, object>> convertToObjectExpression<T, TProp>(Expression<Func<T, TProp>> expression)
{
return Expression.Lambda<Func<T,object>>(
Expression.Convert(expression.Body, typeof(object)),
expression.Parameters);
}
This works fine, except when I'm using the expression with a nullable DateTime (DateTime?) as type of TProp. In that case the following line results in 'me' being null.
me = ((ue != null) ? ue.Operand : null) as MemberExpression;
Does anybody have any idea what can cause this problem?
I have a fiddle that contains the problem.
I would suggest you just go the other way round. Do not relay from your new typesafe method to the old one (with all attached problems), but from the old one to the new method:
// relay old interface to new one for compatibility
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
return GetPropertyInfo<T, object>(expressie);
}
public static PropertyInfo GetPropertyInfo<T, TProp>(Expression<Func<T, TProp>> expressie)
{ /* your code, as you provided it */ }
If you still got problems with this, please provide your calling code
Edit
removed for being irrelevant to the problem
Does anybody have any idea what can cause this problem?
The way you called the new function (with DateTime? cast from a normal DateTime property) and the way you implemented it (via convertToObjectExpression) leads to having two Convert expressions, hence the problem.
In order to fix it, consider the user grek40 advice, and/or change the implementation as follows
public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> expressie)
{
var body = expressie.Body;
while (body.NodeType == ExpressionType.Convert || body.NodeType == ExpressionType.ConvertChecked)
body = ((UnaryExpression)body).Operand;
var me = body as MemberExpression;
var property = me != null ? me.Member as PropertyInfo : null;
if (property == null)
{
throw new InvalidOperationException("Expression does not refer to a property: " + expressie.ToString());
}
return property;
}

Dynamically create lambda search on IQueryable on nested objects

I'm trying to build a dynamic search on nested objects, which will later be sent to EF and SQL Server. So far, I'm able to search on all properties of the first object. Here's a very simplified version:
public class User
{
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
}
public class MyClass<TEntity> where TEntity : class {
public IQueryable<TEntity> applySearch(IQueryable<TEntity> originalList, string propName, string valueToSearch) {
ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
PropertyInfo propertyInfo = typeof(TEntity).GetProperty(propName);
MemberExpression member = Expression.MakeMemberAccess(parameterExpression, propertyInfo);
lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(valueToSearch)), parameterExpression);
return originalList.Where(expression);
}
}
When propName = "Name" everything is fine, but when propName = "Address.City", the propertyInfo is null, and I get this error on the member assignment line:
System.ArgumentNullException: Value cannot be null
I was able to obtain the propertyInfo of the nested property using the solution from this answer:
PropertyInfo propertyInfo = GetPropertyRecursive(typeof(TEntity), propName);
...
private PropertyInfo GetPropertyRecursive(Type baseType, string propertyName)
{
string[] parts = propertyName.Split('.');
return (parts.Length > 1)
? GetPropertyRecursive(baseType.GetProperty(parts[0]).PropertyType, parts.Skip(1).Aggregate((a, i) => a + "." + i))
: baseType.GetProperty(propertyName);
}
But then I get this error on member assignment:
System.ArgumentException: Property 'System.String City' is not defined for type 'User'
This should point to Address instead of User, but I don't know if I'm on right track here, I mean, should I change parameterExpression now?
How can I make a dynamic search on nested objects, so that this can be turned into a lambda expression and later sent to SQL?
After Kobi's advice and a lot of trial and error, I finally got this working. This uses the Universal PredicateBuilder. Here it is:
public class MyClass<TEntity> where TEntity : class
{
public IQueryable<TEntity> ApplySearch(IQueryable<TEntity> originalList, string valueToSearch, string[] columnsToSearch)
{
Expression<Func<TEntity, bool>> expression = null;
foreach (var propName in columnsToSearch)
{
Expression<Func<TEntity, bool>> lambda = null;
ParameterExpression parameterExpression = Expression.Parameter(typeof(TEntity), "p");
string[] nestedProperties = propName.Split('.');
Expression member = parameterExpression;
foreach (string prop in nestedProperties)
{
member = Expression.PropertyOrField(member, prop);
}
var canConvert = CanConvertToType(valueToSearch, member.Type.FullName);
if (canConvert)
{
var value = ConvertToType(valueToSearch, member.Type.FullName);
if (member.Type.Name == "String")
{
ConstantExpression constant = Expression.Constant(value);
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.Call(member, mi, constant);
lambda = Expression.Lambda<Func<TEntity, bool>>(call, parameterExpression);
}
else
{
lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, Expression.Constant(value)), parameterExpression);
}
}
if (lambda != null)
{
if (expression == null)
{
expression = lambda;
}
else
{
expression = expression.Or(lambda);
}
}
}
if (expression != null)
{
return originalList.Where(expression);
}
return originalList;
}
}
private bool CanConvertToType(object value, string type)
{
bool canConvert;
try
{
var cValue = ConvertToType(value, type);
canConvert = true;
}
catch
{
canConvert = false;
}
return canConvert;
}
private dynamic ConvertToType(object value, string type)
{
return Convert.ChangeType(value, Type.GetType(type));
}
Warning in advance - I'm not building the expression, just inspecting its structure.
When I need to dynamically create Expressions, I find it useful to inspect an Expression and copy its structure:
Expression<Func<User, string>> getCity = user => user.Address.City;
Now you can simply debug it, for example in the immediate window (ctrlalti here):
getCity
{user => user.Address.City}
Body: {user.Address.City}
CanReduce: false
DebugView: ".Lambda #Lambda1<System.Func`2[ConsoleApplication1.User,System.String]>(ConsoleApplication1.User $user) {\r\n ($user.Address).City\r\n}"
Name: null
NodeType: Lambda
Parameters: Count = 1
ReturnType: {Name = "String" FullName = "System.String"}
TailCall: false
Here we can see getCity is a Lambda with one parameter. Let's inspect it's body:
getCity.Body
{user.Address.City}
CanReduce: false
DebugView: "($user.Address).City"
Expression: {user.Address}
Member: {System.String City}
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
getCity.Body is a member access - it accesses the member City of the Expression user.Address. Technically that's a PropertyExpression, which is an internal class so we can't even cast to it, but that's OK.
Finally, let's look at that inner expression:
((MemberExpression)getCity.Body).Expression
{user.Address}
CanReduce: false
DebugView: "$user.Address"
Expression: {user}
Member: {ConsoleApplication1.Address Address}
NodeType: MemberAccess
Type: {Name = "Address" FullName = "ConsoleApplication1.Address"}
That's just user.Address.
Now we can build an identical expression:
var addressProperty = typeof (User).GetProperty("Address");
var cityProperty = typeof(Address).GetProperty("City");
var userParameter = Expression.Parameter(typeof (User), "user");
var getCityFromUserParameter = Expression.Property(Expression.Property(userParameter, addressProperty), cityProperty);
var lambdaGetCity = Expression.Lambda<Func<User, string>>(getCityFromUserParameter, userParameter);
Expression.MakeMemberAccess works too, instead of Expression.Property.
Obviously, you'd need to build your expression in a loop, and more dynamically, but the structure is the same.
It might be worth taking a look at Linqkit's predicate builder...
http://www.albahari.com/nutshell/predicatebuilder.aspx
I'd also take a look at Entity SQL...
https://msdn.microsoft.com/en-us/library/vstudio/bb387145(v=vs.100).aspx
You might be reinventing a wheel with the code you're writing.
Also I should comment, in terms of the SQL Server plan caching, unless you have no other choice I wouldn't dynamically build queries. You're better off creating a single query that handles all your cases that SQL Server can cache a plan for, your queries will run a lot slower if every time they're executed no plan is hit in SQL Server's plan cache.

Categories

Resources