c# - Linq List of Objects GroupBy Multiple Property Equality -
i have list<reportobject>
, want able combine elements of list single element based on equality of properties 1 element matching other properties second element in list. in case, want update first element values second element , return list collection of "first elements".
perhaps groupby (or linq in general) isn't right solution here, seem lot cleaner doing traditional foreach
loop , newing second list. want this:
list<reportobject> thelist = new list<reportobject>() { new reportobject() { property1 = "1", property2 = "2" }, new reportobject() { property1 = "2", property2 = "3" } new reportobject() { property1 = "1", property2 = "3" } }; list<reportobject> newlist = new list<reportobject>(); for(int = 0; < thelist.count; i++) { for(int j = + 1; < thelist.count; j++) { if (thelist[i].property1 == thelist[j].property2) { thelist[i].property2 = thelist[j].property2); newlist.add(thelist[i]); thelist.removeat(j); } } } return newlist;
from code, it's newlist
contain items have property1 = property2
:
var newlist = thelist.selectmany((x,i)=> thelist.where((y,j)=>j>i && y.propery2 == x.propery1) .select(a=> new reportobject{ property1=x.property1, property2=x.property1 });
Comments
Post a Comment