c# - Why changing one item in a linq group affects other items in the same group? -
i'm working on c# code similar this:
public class item { public bool flag { get; set; } public string itemname { get; private set;}; item(string _name) { this.itemname = _name; this.flag = false; } } public class mainclass { public static void main() { list itemlist = new list<item>(); itemlist.add(new item("producta"); itemlist.add(new item("producta"); itemlist.add(new item("productb"); var itemgroups = item in itemlist group item item.itemname; foreach (var itemgroup in itemgroups) { foreach (item in itemgroup) { i.flag = true; break; } } } }
what want achieve here change 1 item in group without touching other items in same group. above code, i'm expecting 1st item itemname "producta" has flag set true. result both items (with itemname "producta") have flag being set true.
i'm new linq. i'm doing wrong? or how can achieve goal here?
many thanks!!!
doug
i not able reproduce scenario has each item unique of type item
. may in code, have accidentally added item twice list like
item item1 = new item("producta"); listitem.add(item1); .... listitem.add(item1);
in case @ later point of time, if updated item itemname "producta", change reflected in 2 elements of list. please check case.
Comments
Post a Comment