0

I want to implement expression tree for integers as in below query:

Select * From TableName Where PKID In (1,2,3,4,5)

The relevant extracted code:

protected Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue, string propertyType)
{
    var parameter = Expression.Parameter(typeof(T), "type");
    switch (propertyType)
    {
        case "string":
        case "string?":
            var stringProperty = Expression.Property(parameter, propertyName);
            var stringValue = Expression.Constant(propertyValue.Trim(), typeof(string));
            MethodInfo stringMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
            var stringContainsMethod = Expression.Call(stringProperty, stringMethod, stringValue);
            return Expression.Lambda<Func<T, bool>>(stringContainsMethod, parameter);

        case "int?":
            var intProperty = Expression.Property(parameter, propertyName);
            var intValues = Expression.Constant(propertyValue.Trim(), typeof(string));
            MethodInfo intMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
            var intContainsMethod = Expression.Call(intProperty, intMethod, intValues); // this line gives error
            return Expression.Lambda<Func<T, bool>>(intContainsMethod, parameter);
    }
}

// Calling above function for string works. Products is a table in Database and Entity framework generated the class based on database first.
var filterLambda = GetExpression<Products>("ProductName", "chicken", "string?"); // No error here, works great

// Calling above function for int gives error
var filterLambda = GetExpression<Products>("ProductId", "1,2,3,4,5", "int?"); // Error here

I get error:

Method 'Boolean Contains(System.String)' declared on type 'System.String' cannot be called with instance of type 'System.Int32'

Of course it is pretty much clear that the column ProductId is integer and the value is string. Integer doesnt have contains so I thought I should use strings contains method. Please can any one tell me how do I solve this?

svick
  • 236,525
  • 50
  • 385
  • 514
suomi-dev
  • 848
  • 1
  • 13
  • 22
  • 1. What does MVC have to do with this? You're showing SQL, so you're using some ORM? Which one, EF? 2. You show some SQL, but your `string` code can't possibly create that, it might create something like `WHERE ProductName LIKE '%chicken%'`, depending on usage. Are you sure your string code works the way you want? – svick Mar 15 '15 at 19:04
  • Is this question useful to you? http://stackoverflow.com/q/2973755/622391 – Simon MᶜKenzie Mar 15 '15 at 22:26
  • I am using Entity Framework 5. And I am using expression trees for building dynamic conditions. That question is not useful because it doesn't uses expression trees. I don't want to hard code conditions. I am storing the search filters in database so it will load dynamically from database. Only problem is with integers. – suomi-dev Mar 16 '15 at 04:20
  • @svick 1. I am using ORM EF in MVC4. 2. I show sql just for example. I want to implement expression tree for that SQL i showed. Yes with string it works. because there is no type mismatch. only problem is that Int32 doesnt have contain method so what should i use instead? – suomi-dev Mar 18 '15 at 07:29

0 Answers0