开发者

LINQ and OR Criteria

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜