在Linq到实体中组合谓词

时间:2012-06-04 01:59:52

标签: c# linq-to-entities

我想动态构建我的条件列表。这是我的代码片段:

protected Expression<Func<event_info, bool>> _wherePredicate = c => true;

public void main() 
{

 _wherePredicate = _wherePredicate.And(c => c.createdby == 6);
 _wherePredicate = _wherePredicate.And(c => c.isdeleted == 0);

 var query = from ev in dataConnection.event_info
                       where ev.isdeleted == 0
                       select ev;
 Results = query.Where(_wherePredicate).ToList(); 
}

除非这不起作用,因为linq-to-entities不支持Invoke方法。

在linq-to-entities中组合谓词的好方法是什么?

1 个答案:

答案 0 :(得分:4)

原来,你需要添加这个:

Results = query。 AsExpandable .Where(_wherePredicate).ToList();

然后它神奇地起作用!

我遵循了这个教程: http://www.albahari.com/nutshell/predicatebuilder.aspx