Sai*_*int 15 c# linq comparison
List<int> one //1, 3, 4, 6, 7
List<int> second //1, 2, 4, 5
Run Code Online (Sandbox Code Playgroud)
如何从第二个列表中的一个列表中获取所有元素?
在这种情况下应该是:1,4
我当然谈论没有foreach的方法.而是linq查询
slo*_*oth 49
您可以使用Intersect方法.
var result = one.Intersect(second);
Run Code Online (Sandbox Code Playgroud)
例:
void Main()
{
List<int> one = new List<int>() {1, 3, 4, 6, 7};
List<int> second = new List<int>() {1, 2, 4, 5};
foreach(int r in one.Intersect(second))
Console.WriteLine(r);
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
4
| 归档时间: |
|
| 查看次数: |
28575 次 |
| 最近记录: |