PredicateBuilder创建条件列表

时间:2012-06-01 21:14:37

标签: c# linq linq-to-sql

我有这样的事情:

class myClass {
  protected ICollection<Expression<Func<event_info, bool>>> _whereConditionsList = new List<Expression<Func<event_info, bool>>>();

  public void main() {
    _whereConditionsList.Add(ev => ev.createdby == 6);
    var query = from ev in dataConnection.event_info
                where ev.isdeleted == 0
                select ev;
    if (_whereConditionsList.Count() > 0) {
      var predicate = PredicateBuilder.True<event_info>();

      foreach (var whereCond in _whereConditionsList) {
        predicate = predicate.And(whereCond);
      }
      query = query.Where(predicate);
    }
  evInfo = query.ToList(); 
}

但它实际上将其转换为SQL代码时遇到了麻烦,因为它会在每次查询时抛出此错误.ToList():

System.NotSupportedException: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.
 ...
 at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
 ...

我的方法有什么问题?

1 个答案:

答案 0 :(得分:0)

结果必须这样做: Combine Predicates in Linq-to-entities

概要:query.AsExpandable()。Where()