Building up a query expression dynamically or
I'm looking at porting logic from a stored procedure to an EF model. 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 Sea开发者_高级运维rcher class. I'm applying the constraints against a set of data 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')
).
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). 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.
Try to look at it.
精彩评论