我有两个清单:
List<int> a = new List() { 1, 2, 3 }
List<int> b = new List() { 4, 5, 6 }
我有这种方法......
public bool DoesExist(List<int> a, List<int> b)
{
foreach (var item in a)
{
if (b.Contains(item)) { return true; }
}
return false;
}
该方法应该为DoesExist(a,b)
返回false如果b是{3,4,5,6},它应该返回true ...
在Linq有一个更清洁的单行方式吗?
感谢
答案 0 :(得分:6)
LINQ的一种可能性:
return a.Intersect(b).Any();
答案 1 :(得分:4)