开发者

How to add an in command to a where clause in LINQ?

If I have a where clause as follows:

Where item.field == "value"

How can I change the statement in LINQ to be:

   Where item.field in ("value1","value2","value3")

Seems simp开发者_运维知识库le, not working.

Thanks in advance


Declare your "in" values collection in a variable first:

var collection = new[] { "value1","value2","value3" };

And then use it in query:

...
where collection.Contains(item.field)
...


Or in lambda syntax(say you have a collection you are searching through):

var lookup = new[] { "value1","value2","value3" };
var result = collection.Where(x=> lookup.Contains(x)).ToArray();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜