score:2

Accepted answer

the call to where does not modify the original linq query stored in collection in any way. instead it creates a new ienumerable<t> where is a subset of the original query. the subset will have the same relative ordering as the original set in collection

the only way this could cause a problem is if the value collection is a linq where which has a where clause, or is otherwise filtered, base on the value of property. if that is the case and you want to preserve the order force collection into a non-lazy evaluated structure via .tolist.

collection = collection.tolist();

score:1

as long as the objects in the collection are mutable, your code should work fine.

by the way, firstordefault takes an optional filter parameter, so you can write collection.firstordefault(i => i.id == someid).


Related Query

More Query from same tag