c# - Logic behind updating object List using linq -
i have list of poco objects, why next code:
elements.where(x => x.param1 == "m").select(x => x.param2= "").tolist();
(tl;dr; sets param2 = "" on every element param1 equals m)
updates enumerable while one:
elements.where(x => x.param1 == "m").select(x => x.param2= "");
does not update it?
notice i'm doing neither elements = elements.where...
nor var results = elements.where...
your sec code snippet without tolist
query. need iterate execute it. calling tolist
executes original query , since in select
modifying property of object, see effect (or side effect) in original list. related parameter passing in c#. since lambda look in select
anonymous method receiving parameter object of list. later when modify 1 of it's property see effect.
similarly if seek set object null
not see side effect.
.select(x => x = null).tolist();
c# linq
No comments:
Post a Comment