简体   繁体   English

检查是否列表<t>包含任何其他列表</t>

[英]Check if list<t> contains any of another list

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.我想遍历参数列表并检查源属性是否等于任何 myStrings 数组。 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.我可以用嵌套的 foreach 来做到这一点,但我想学习如何以更好的方式做到这一点,因为我一直在玩 linq 并且喜欢可枚举的扩展方法,比如 where 等,所以嵌套的 foreach 感觉不对。 Is there a more elegant preferred linq/lambda/delegete way to do this.是否有更优雅的首选 linq/lambda/delegete 方式来执行此操作。

Thanks谢谢

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 parameters to source and then use Intersect which 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) :在较大的集合上更快地执行是将parameters投影到source ,然后使用Intersect ,它内部使用HashSet<T>因此,对于第一种方法(相当于两个嵌套循环),您可以进行检查而不是 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# 样式指南。

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!");
}

If both the list are too big and when we use lamda expression then it will take a long time to fetch .如果两个列表都太大,并且当我们使用 lamda 表达式时,那么 fetch 将需要很长时间。 Better to use linq in this case to fetch parameters list:在这种情况下最好使用 linq 来获取参数列表:

var items = (from x in parameters
                join y in myStrings on x.Source equals y
                select x)
            .ToList();
list1.Select(l1 => l1.Id).Intersect(list2.Select(l2 => l2.Id)).ToList();
var list1 = await _service1.GetAll();
var list2 = await _service2.GetAll();

// Create a list of Ids from list1
var list1_Ids = list1.Select(l => l.Id).ToList();

// filter list2 according to list1 Ids
var list2 = list2.Where(l => list1_Ids.Contains(l.Id)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM