I have two lists
List1 Only two property. Cant use Dictionary since there might be duplicate keys. The combination of Property1 and Property2 is unique.
public class List1
{
public string Property1 { get; internal set; }
public string Property2 { get; internal set; }
}
public class List2
{
public string Property1 { get; internal set; }
public string Property2 { get; internal set; }
public string Property3 { get; internal set; }
}
List<List1> mylist1 = new List<List1>() {
new List1() {Property1="664",Property2="Ford" },
new List1() {Property1="665",Property2="Ford" },
new List1() {Property1="664",Property2="Toyota" },
};
List<List2> mylist2 = new List<List2>() {
new List2() {Property1="664",Property2="Ford" ,Property3="USA"},
new List2() {Property1="665",Property2="Ford" ,Property3="USA"},
new List2() {Property1="664",Property2="Toyota" ,Property3="USA"},
new List2() {Property1="666",Property2="Toyota" ,Property3="USA"},
};
I need to get the matching items in mylist1 and mylist2. The match should happen only on Property1 and Property2. Property3 in the mylist2 can be ignored during comparison.
Currently I use
var matchingCodes = mylist1.Where(l1 => mylist2.Any(l2 => (l2.Property1 == l1.Property1 && l2.Property2==l1.Property2))).ToList();
which works perfectly fine. But is there a better way/ fastest way to do this?
I can change List1 to any other Type. but not List2.