Expression<Func<TObject, bool>>与Func<TObject, bool>的区别
Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式比如: Expression编译后就会变成delegate,才能运行。 Expression<Func<int, bool>> ex = x=>x < 100; Func<int, bool> func = ex.Compile(); 然后你就可以调用func: func(5) //-返回 true func(200) //- 返回 false 而表达式是不能直接调用的。 1 2 3 4 5 6 7 8 9 10 11 12 案例:不正确的查询代码造成的数据库全表查询。 //错误的代码 Func<QuestionFeed, bool > predicate = null ; if (type == 1) { predicate = f => f.FeedID == id && f.IsActive == true ; } else { predicate = f => f.FeedID == id; } //_questionFeedRepository.Entities的类型为IQueryable<QuestionFeed> _questionFeedRepository.Entities.Where(predicate);