c# - why object in collection of System.Linq.Enumerable.WhereSelectListIterator cannot be changed -
i use linq select data , returned type whereselectlistiterator. find object in whereselectlistiterator cannot modified, e.g., set null or update property of value. design? how make possible?
using system; using system.collections.generic; using system.linq; namespace linqtest { class program { static void main(string[] args) { var project = new project() { sections = new list<section>() { new section() { outdated = new char[2]{'a','d'} }, new section() { outdated = new char[2]{'a','d'} }, new section() { outdated = new char[2]{'a','d'} } } }; var dates = project.sections.select(t=>(new mystring(t.outdated))); console.write(dates.elementat(0).description); dates.elementat(0).description = "123"; console.write(dates.elementat(0).description); console.read(); } } class mystring { public mystring(char[] abc) { description = new string(abc).substring(1); } public string description { set; get; } } class project { public ienumerable<section> sections; } internal class section { public char[] outdated; } }
linq acts using yield
operator on collection. if modify collection, invalidates iterator.
to workaround this, consider converting concrete class rather working ienumerable<t>
calling toarray()
or tolist()
on ienumerable<t>
.
Comments
Post a Comment