Returning a nested generic expression <Func <T, bool >>

Error message: "Type or namespace name" T "not found."

???

public static Expression<Func<T, bool>> MakeFilter(string prop, object val) { ParameterExpression pe = Expression.Parameter(typeof(T), "p"); PropertyInfo pi = typeof(T).GetProperty(prop); MemberExpression me = Expression.MakeMemberAccess(pe, pi); ConstantExpression ce = Expression.Constant(val); BinaryExpression be = Expression.Equal(me, ce); return Expression.Lambda<Func<T, bool>>(be, pe); } 

Related links:

Using reflection to address the Linqed property

http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/df9dba6e-4615-478d-9d8a-9fd80c941ea2/

Creating a Runtime Generic Func <T>

+7
c # lambda linq
source share
3 answers

You need to make this method general:

 public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val) -+- ^ +- this 
+10
source share

There is no general argument specific to your method. You must define one ( MakeFilter<T> ):

 public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val) { ParameterExpression pe = Expression.Parameter(typeof(T), "p"); PropertyInfo pi = typeof(T).GetProperty(prop); MemberExpression me = Expression.MakeMemberAccess(pe, pi); ConstantExpression ce = Expression.Constant(val); BinaryExpression be = Expression.Equal(me, ce); return Expression.Lambda<Func<T, bool>>(be, pe); } 
+3
source share

This method should be declared as general ( MakeFilter<T> ):

 public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val) 

Otherwise, how else would the caller indicate that T ?

+2
source share

All Articles