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?
list1 = ['item1','item2','item3'] list2 = ['item4','item5','item3'] if list1 contains any items also in list2: print("Duplicates found.") else: print("No duplicates found.")
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.
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.
You could use a nested Any() for this check which is available on any Enumerable : bool hasMatch = myStrings. Any(x => parameters. Any(y => y.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With