How to convert an Expression> to a Predicate

前端 未结 3 1054
醉话见心
醉话见心 2021-01-30 10:32

I have a method that accepts an Expression> as a parameter. I would like to use it as a predicate in the List.Find() method, but I can\'t

3条回答
  •  心在旅途
    2021-01-30 10:52

    I'm not seeing the need for this method. Just use Where().

     var sublist = list.Where( expression.Compile() ).ToList();
    

    Or even better, define the expression as a lambda inline.

     var sublist = list.Where( l => l.ID == id ).ToList();
    

提交回复
热议问题