如果我有一个where子句如下:
Where item.field == "value"
如何将LINQ中的语句更改为:
Where item.field in ("value1","value2","value3")
似乎很简单,不起作用。
提前致谢
答案 0 :(得分:6)
首先在变量中声明“in”值集合:
var collection = new[] { "value1","value2","value3" };
然后在查询中使用它:
...
where collection.Contains(item.field)
...
答案 1 :(得分:1)
或者在lambda语法中(假设你有一个正在搜索的集合):
var lookup = new[] { "value1","value2","value3" };
var result = collection.Where(x=> lookup.Contains(x)).ToArray();