c# - Linq to compare two lists of objects where one object has several lists -
public class myobject1 { public guid g1; public guid g2; public guid g3; } public class myobject2 { public guid g4; } list<myobject1> list1; list<myobject2> list2;
i linq query return myobject1 objects in list1 myobject1.g2 == myobject2.g4 myobject2 objects live in list 2
i have written code in numerous steps think in 1 iteration.
so
var n = list1.select(p=> p.g2).tolist() var o = list2.intersect(n)
but need research list1 using o again awkward
it sounds want join:
var query = x1 in list1 join x2 in list2 on x1.g2 equals x2.g4 select x1;
or in extension method syntax:
var query = list1.join(list2, x1 => x1.g2, x2 => x2.g4, (x1, x2) => x1);
note give items list1
- if need corresponding item list2
well, that's simple:
var query = x1 in list1 join x2 in list2 on x1.g2 equals x2.g4 select new { x1, x2 };
Comments
Post a Comment