Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if one list contains any elements from another

Tags:

c#

linq

I am just trying to return true if one list contains any of the Name/Value from list2:

This would be my structure:

public class TStockFilterAttributes
{
    public String Name { get; set; }
    public String Value { get; set; }
}

List<TStockFilterAttributes> List1 = new List<TStockFilterAttributes>();
List<TStockFilterAttributes> List2 = new List<TStockFilterAttributes>();

This should return true:

List1.Add(new TStockFilterAttributes { Name = "Foo", Value = "Bar" });
List2.Add(new TStockFilterAttributes { Name = "Foo", Value = "Bar" });

But this would return false because Name && Value don't match:

List1.Add(new TStockFilterAttributes { Name = "Foo", Value = "Bar" });
List2.Add(new TStockFilterAttributes { Name = "Foo", Value = "Foo" });

Each list could contains lots of different values and I just need to know if any one of List1 matches any one in List2.

I have tried using:

return List1.Intersect(List2).Any();

but this seems to return false in all cases, I am assuming this is because I am holding a class in List rather than a simple int / string?

like image 539
webnoob Avatar asked Nov 21 '12 14:11

webnoob


People also ask

How do you check if a list contains any value from another list?

list1 = ['item1','item2','item3'] list2 = ['item4','item5','item3'] if list1 contains any items also in list2: print("Duplicates found.") else: print("No duplicates found.")

How do you check if a list contains any element of another list Java?

contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

How do you check if a list is present in another list in Python?

Given two lists A and B, write a Python program to Check if list A is contained in list B without breaking A's order. A more efficient approach is to use List comprehension. We first initialize 'n' with length of A. Now use a for loop till len(B)-n and check in each iteration if A == B[i:i+n] or not.

Can I check if a list contains any item from another list C #?

You could use a nested Any() for this check which is available on any Enumerable : bool hasMatch = myStrings. Any(x => parameters. Any(y => y.


1 Answers

Override Equals and GetHashCode implementation for your class:

public class TStockFilterAttributes
{
    public String Name { get; set; }
    public String Value { get; set; }

    public override bool Equals(object obj)
    {
        TStockFilterAttributes other = obj as TStockFilterAttributes;
        if (obj == null)
            return false;

        return Name == obj.Name && Value == obj.Value;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode() ^ Value.GetHashCode();
    }
}

Or provide a comparer to Intersect function.

like image 185
Sergey Berezovskiy Avatar answered Oct 15 '22 20:10

Sergey Berezovskiy