Reputation: 109
Currently I am working with something similar to the following:
if(listA.All(x => x.myHouseNumber == "1" || x.myValue == "2")
{
//do something
}
listA is a list of elements (myName, myHouseNumber, myStreet)
basically, I want the condition to be true if every myHouseNumber in listA is found in listB which is just a list of numbers
So if listA contains Bill,1,Main and Ted,2,Second and listB contains 1,2,3 the condition is true because 1 and 2 are found within list B. if listB contained 1,5,9 the condition would be false because 2 is missing.
I am having trouble understanding how to compare a list with a single item to a single element in a list with several items.
The end result I am hoping would be something like this which I cannot seem to get to work
if(listA.All(x => x.myHouseNumber.Contains(listB.Any())))
{
//do something
}
Hopefully someone will understand what I am going for and be able to provide some assistance
Thanks in advance!
Upvotes: 0
Views: 49
Reputation: 722
If I understand is correct, you should use the listB contains myHouseNumber.
the demo code.
public class test {
public string myName;
public string myHouseNumber;
public string myStreet;
}
var listA = new List<test>
{ new test { myHouseNumber = "1", myName = "Bill", myStreet = "Main" },
new test { myHouseNumber = "2", myName = "Ted", myStreet = "Second" }
};
var listB = new List<string> { "1", "2", "3" };
var listC = new List<string> { "1", "5", "9" };
// it is true
listA.All(p => listB.Contains(p.myHouseNumber))
// it is false
istA.All(p => listC.Contains(p.myHouseNumber))
Upvotes: 2