【问题标题】:Dynamic change body of lambda exprssion?lambda表达式的动态变化体?
【发布时间】:2013-11-28 13:05:40
【问题描述】:

例如我有以下代码

public class Person
{
    public bool FirstNameIsActive { get; set; }
    public bool SecondNameIsActive { get; set; }
}    

如果我想按属性过滤FirstNameIsActive

Func<Person, bool> predicate1 = x => x.FirstNameIsActive == true;

如果我想按属性过滤SecondNameIsActive

Func<Person, bool> predicate2 = x => x.SecondNameIsActive == true;

我想在运行时将我的谓词更改为

Func<Person, bool> predicate = x => x.PropertyThatIWant == true;

【问题讨论】:

    标签: c# .net lambda expression func


    【解决方案1】:

    您可以利用修改后的闭包。

    //property selector
    Func<Person, Boolean> propertySelector = person => person.FirstNameIsActive;
    
    //your predicate
    Func<Person, Boolean> predicate = person => propertySelector(person) == true;
    
    //new person with true, false properties.
    Person p = new Person() {FirstNameIsActive = true,SecondNameIsActive = false};
    
    Console.WriteLine(predicate(p).ToString()); //prints true
    
    //change the property selector
    propertySelector = person => person.SecondNameIsActive;
    
    //now the predicate uses the new property selector
    Console.WriteLine(predicate(p).ToString()); //prints false
    

    【讨论】:

      【解决方案2】:

      还有我的解决方案

      public class Person
      {
          public bool FirstNameIsActive { get; set; }
          public bool SecondNameIsActive { get; set; }
      }
      
      public List<Person> list = new List<Person>() { 
          new Person() { 
              FirstNameIsActive = true, 
              SecondNameIsActive = false 
          }, 
          new Person() { 
              FirstNameIsActive = false, 
              SecondNameIsActive = true 
          } 
      };
      
      private IQueryable<Person> Filter(PropertyInfo property, bool isActive)
      {
          IQueryable<Person> queryableData = list.AsQueryable<Person>();
          //create input parameter
          ParameterExpression inputParam = Expression.Parameter(typeof(Person));
          //left contition
          Expression left = Expression.Property(inputParam, property);
          //right condition
          Expression right = Expression.Constant(isActive, typeof(bool));
          //equals
          Expression e1 = Expression.Equal(left, right);
          //create call
          MethodCallExpression whereCallExpression = Expression.Call(
                                                                  typeof(Queryable),
                                                                  "Where",
                                                                  new Type[] { queryableData.ElementType },
                                                                  queryableData.Expression,
                                                                  Expression.Lambda<Func<Person, bool>>(e1, new ParameterExpression[] { inputParam }));
          //execute and return
          return queryableData.Provider.CreateQuery<Person>(whereCallExpression);
      }
      
      private void test()
      {
          Filter(typeof(Person).GetProperty("FirstNameIsActive"), true);
          Filter(typeof(Person).GetProperty("SecondNameIsActive"), true);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多