简体   繁体   English

动态构建查询表达式或

[英]Building up a query expression dynamically or

I'm looking at porting logic from a stored procedure to an EF model. 我正在研究将逻辑从存储过程移植到EF模型。 The stored procedure optionally accepted a list of departments which would be searched. 该存储过程可以选择接受要搜索的部门列表。 Effectively I'm dynamically building the search criteria (or trying to..). 有效地,我正在动态地建立搜索条件(或试图..)。

I've got a parameter class which contains a List< string> DeptCodes which is passed to my Searcher class. 我有一个参数类,其中包含一个List< string> DeptCodes ,该参数传递给我的Searcher类。 I'm applying the constraints against a set of data IQueryable< SearchResult> dataList . 我将约束应用于一组数据IQueryable< SearchResult> dataList If DeptCodes contains any items I want to apply a chained set of or-ed constraints (in SQL where (DeptCode in 'ABC' or DeptCode in 'DEF') ). 如果DeptCodes包含任何项目,我想应用一组链式or-ed约束(在SQL where (DeptCode in 'ABC' or DeptCode in 'DEF') )。

I guess I want to create an expression tree but I'm not sure where to start (my LINQ skills aren't quite up to speed at the moment). 我想我想创建一个表达式树,但是我不确定从哪里开始(我的LINQ技能目前还不太快)。 Can anyone point me in the right direction or give me a little sample to get me started? 谁能指出我正确的方向,还是给我一些示例以帮助我入门?

Thanks in advance 提前致谢

I think PredicateBuilder could be a good solution. 我认为PredicateBuilder可能是一个很好的解决方案。

Try to look at it . 尝试看一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM