add generic list item to another existing list item using c# -
i have generic list filled items. in list have 1 column unique (like id). , have generic list other items , same id column. way fill items lists:
foreach (string s in l1) { gridviewsource src = new gridviewsource(); src.test = "id" + s; list.add(src); } foreach (string s in l2) { gridviewsource src = new gridviewsource(); src.test = "id" + s; src.test2 = "somerandomtext" + s; list2.add(src); }
what i'm trying add items list2
list
if have same id. should add item list2
id "id3" item "id3" of list
. merging. don't want add them bottom of list or insert them between items. tried concat
method it's adding items add end of list:
list = list.concat(list2).tolist();
edit:
i try explain way:
my list
looks this:
[0] => test = "id1", test2 = "" [1] => test = "id2", test2 = "" [2] => test = "id3", test2 = "" [3] => test = "id4", test2 = "" [4] => test = "id5", test2 = ""
and list2
looks this:
[0] => test = "id1", test2 = "somerandomtext1" [1] => test = "id2", test2 = "somerandomtext2" [2] => test = "id3", test2 = "somerandomtext3"
and when merge lists, should this:
[0] => test = "id1", test2 = "somerandomtext1" [1] => test = "id2", test2 = "somerandomtext2" [2] => test = "id3", test2 = "somerandomtext3" [3] => test = "id4", test2 = "" [4] => test = "id5", test2 = ""
but looks this:
[0] => test = "id1", test2 = "" [1] => test = "id2", test2 = "" [2] => test = "id3", test2 = "" [3] => test = "id4", test2 = "" [4] => test = "id5", test2 = "" [5] => test = "id1", test2 = "somerandomtext1" [6] => test = "id2", test2 = "somerandomtext2" [7] => test = "id3", test2 = "somerandomtext3"
any suggestions?
so need merge on item level, not on list level.
i'm sure there clever way using linq, don't have experience linq, can suggest simple nested loops:
foreach (gridviewsource src1 in list1) { foreach(gridviewsource src2 in list2) { if(src1.test1 == src2.test1) { src1.test2 = src2.test2; } } }
Comments
Post a Comment