C# 检查 list<t> 是否包含任何其他列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11092930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:27:12  来源:igfitidea点击:

Check if list<t> contains any of another list

c#listloopsc#-4.0subset

提问by gdp

I have a list of parameters like this:

我有一个这样的参数列表:

public class parameter
{
    public string name {get; set;}
    public string paramtype {get; set;}
    public string source {get; set;}
}

IEnumerable<Parameter> parameters;

And a array of strings i want to check it against.

和我想检查的字符串数组。

string[] myStrings = new string[] { "one", "two"};

I want to iterate over the parameter list and check if the source property is equal to any of the myStrings array. I can do this with nested foreach's but i would like to learn how to do it in a nicer way as i have been playing around with linq and like the extension methods on enumerable like where etc so nested foreachs just feel wrong. Is there a more elegant preferred linq/lambda/delegete way to do this.

我想遍历参数列表并检查源属性是否等于任何 myStrings 数组。我可以用嵌套的 foreach 来做到这一点,但我想学习如何以更好的方式做到这一点,因为我一直在玩 linq 并且喜欢可枚举的扩展方法,比如 where 等,所以嵌套的 foreachs 感觉不对。有没有更优雅的首选 linq/lambda/delegete 方式来做到这一点。

Thanks

谢谢

采纳答案by BrokenGlass

You could use a nested Any()for this check which is available on any Enumerable:

您可以使用嵌套Any()进行此检查,该检查可用于任何Enumerable

bool hasMatch = myStrings.Any(x => parameters.Any(y => y.source == x));

Faster performing on larger collections would be to project parametersto sourceand then use Intersectwhich internally uses a HashSet<T>so instead of O(n^2) for the first approach (the equivalent of two nested loops) you can do the check in O(n) :

在较大的集合上更快地执行将是投影parameterssource然后使用Intersectwhich 在内部使用HashSet<T>so 而不是 O(n^2) 对于第一种方法(相当于两个嵌套循环),您可以在 O(n) 中进行检查:

bool hasMatch = parameters.Select(x => x.source)
                          .Intersect(myStrings)
                          .Any(); 

Also as a side comment you should capitalize your class names and property names to conform with the C# style guidelines.

另外作为旁注,您应该将类​​名和属性名大写以符合 C# 样式指南。

回答by Masoud Darvishian

Here is a sample to find if there are match elements in another list

这是一个示例,用于查找另一个列表中是否存在匹配元素

List<int> nums1 = new List<int> { 2, 4, 6, 8, 10 };
List<int> nums2 = new List<int> { 1, 3, 6, 9, 12};

if (nums1.Any(x => nums2.Any(y => y == x)))
{
    Console.WriteLine("There are equal elements");
}
else
{
    Console.WriteLine("No Match Found!");
}

回答by Umang Agarwal

If both the list are too big and when we use lamda expression then it will take a long time to fetch . Better to use linq in this case to fetch parameters list:

如果两个列表都太大,并且当我们使用 lamda 表达式时,那么 fetch 将需要很长时间。在这种情况下最好使用 linq 来获取参数列表:

var items = (from x in parameters
                join y in myStrings on x.Source equals y
                select x)
            .ToList();