Check if one list contains any elements from another

c#, linq

Solution

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.

Problem

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?

Original source