如何使用C#中的LINQ检查List是否包含另一个List中存在的元素?我不想使用for/while循环.
所以,如果List1有A,B,C和List2有B,1,2,那么我会返回true.
das*_*ght 34
试试这个:
List<string> a = ...
List<string> b = ...
var inComon = a.Intersect(b).Any();
Run Code Online (Sandbox Code Playgroud)
hor*_*rgh 14
List<string> l1 = new List<string> { "1", "2" };
List<string> l2 = new List<string> { "1", "3" };
var result = l2.Any(s => l1.Contains(s));
Run Code Online (Sandbox Code Playgroud)
我会说Intersect方法(见dasblinkenlight的回答)+ Any必须比Contains+ 更好Any.使用它Any比定义更好Count.
| 归档时间: |
|
| 查看次数: |
18635 次 |
| 最近记录: |