问题描述:

how can I programmatically create an EF query (extension methods with lamda). I understand the and criteria. Here is the pseudo-code:

var query = repository.Where(x => x.Name == "aName");

foreach(string filter in filters)

{

query = query.Where(x => x.FilterValue.Contains(filter))

}

But what I want is not an and operator. I would like an or operator. How do I do this? How can I create complex criteria Trees in code?

网友答案:

http://www.albahari.com/nutshell/predicatebuilder.aspx

PrdicateBuilder is a nice solution. But it is complicated and not so easy to understand. See Link to other question in comment.

Also found this useful: Sometimes it is convenient to come from the other direction:

string[] filter = {"A", "B"};
var returnValue = repository
                .Where(x => x.Name == "aName")
                .Where(x => filter.Any(f => (x.FilterValue).Contains(f)))
                .ToList();
相关阅读:
Top