检查两个List< INT>的为同一号码号码、两个、List、LT

2023-09-04 02:34:53 作者:我去你马勒戈壁·

我有两个表的,我要检查相应的数字。

I have two List's which I want to check for corresponding numbers.

例如

List<int> a = new List<int>(){1, 2, 3, 4, 5};
List<int> b = new List<int>() {0, 4, 8, 12};

应该给4的结果。 有没有一种简单的方法来做到这一点,而无需通过列表太多的循环?

Should give the result 4. Is there an easy way to do this without too much looping through the lists?

我在3.0,我需要这个所以没有LINQ的项目。

I'm on 3.0 for the project where I need this so no Linq.

推荐答案

您可以使用.NET 3.5 .Intersect()扩展方法: -

You can use the .net 3.5 .Intersect() extension method:-

List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
List<int> b = new List<int>() { 0, 4, 8, 12 };

List<int> common = a.Intersect(b).ToList();