Creating Dynamic Predicates- passing in property to a function as parameter

后端 未结 1 830
Happy的楠姐
Happy的楠姐 2020-12-15 00:55

I am trying to create dynamic predicate so that it can be used against a list for filtering

 public class Feature
 {
   public string Color{get;set;}
   publ         


        
1条回答
  •  悲哀的现实
    2020-12-15 01:40

    public Predicate GetFilter(
        Expression> property,
        T value,
        string condition)
    {
        switch (condition)
        {
        case ">=":
            return
                Expression.Lambda>(
                    Expression.GreaterThanOrEqual(
                        property.Body,
                        Expression.Constant(value)
                    ),
                    property.Parameters
                ).Compile();
    
        default:
            throw new NotSupportedException();
        }
    }
    

    Any questions? :-)

    0 讨论(0)
提交回复
热议问题