开发者

LINQ and OR Criteria

开发者 https://www.devze.com 2023-03-30 02:32 出处:网络
how can I programmatically create an EF query (extension methods with lambda). I understand the and criteria. Here is the pseudo-code:

how can I programmatically create an EF query (extension methods with lambda). 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 creat开发者_JS百科e 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消