Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am tring to create dynamic list i want to filter items from list

this function perfectly work with string but other than string like Int32 or DataTime it gives exception

C#
public static List<T> Filter<T>
         (this List<T> source, string columnName,
          string compValue)
     {
         ParameterExpression parameter = Expression.Parameter(typeof(T), "x");
         Expression property = Expression.Property(parameter, columnName);
         Expression constant = Expression.Constant(compValue);
        if (property.Type == typeof(string)) //this is for testing
         {

             Expression<Func<T, bool>> predicate =

                 GetExpression<T>(columnName, compValue);

             Func<T, bool> compiled = predicate.Compile();
             return source.Where(compiled).ToList();
         }


         return null;
     }

static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
     {
         var parameterExp = Expression.Parameter(typeof(T), "type");
         var propertyExp = Expression.Property(parameterExp, propertyName);
         MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
         var someValue = Expression.Constant(propertyValue, typeof(string));
         var containsMethodExp = Expression.Call(propertyExp, method, someValue); // on this line i am getting error if i remove if condition of type checking

         return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
     }


Error

declared on type 'System.String' cannot be called with instance of type 'System.Int32' c#

so i want some function that can handle this issue

actually i tring to create dynamic list for datatable Jquery in mvc
http://www.datatables.net/[^]

i am stuck on one of my column is integer so i am not able to filter on that column
so i am searching
Posted
Updated 7-Aug-14 0:41am
v3
Comments
johannesnestler 5-Aug-14 7:25am    
Did you read your code? You write: if (property.Type == typeof(string))
and wonder that it just works with strings?.
Member-515487 5-Aug-14 8:21am    
yes i know
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
on this line it gives error
johannesnestler 5-Aug-14 8:51am    
You wanted to reply to me? - if yes pleas use the reply button, otherwise I don't get informed about your comment. anyway I don't understand what you trying to say (the mentioned line dosn't appear in your code - If you suspect the Problem in the calling code - Show this code! and damn, I say it: If you have the original exception text - then post it!).
Member-515487 7-Aug-14 6:40am    
Method 'Boolean Contains(System.String)' declared on type 'System.String' cannot be called with instance of type 'System.Int32'

1 solution

I change this now it working as expected
for string i want like
other than string i want equal
C#
static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue)
    {
        var parameterExp = Expression.Parameter(typeof(T), "type");
        var propertyExp = Expression.Property(parameterExp, propertyName);
        if (propertyExp.Type == typeof(string))
        {
            MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
            var someValue = Expression.Constant(propertyValue, typeof(string));
            var containsMethodExp = Expression.Call(propertyExp, method, someValue);

            return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
        }
        else
        {
            var converter = TypeDescriptor.GetConverter(propertyExp.Type);
           var result = converter.ConvertFrom(propertyValue);
            var someValue = Expression.Constant(result);
            var containsMethodExp = Expression.Equal(propertyExp, someValue);
            return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);
        }
    }
 
Share this answer
 
v4
Comments
Rezwan Ul 23-Mar-21 9:21am    
Thank you. It solved one of my similar problems. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900