score:2
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)
.
Source: stackoverflow.com
Related Query
- How to modify an item in an IEnumerable collection and retain its order
- How to order a collection and its subcollection using LINQ?
- How to add an item in a collection using Linq and C#
- How to order a collection by a property first and then by another property regardless of tie on first property?
- Why EF can't sort items by the order of another collection and how to workaround?
- How to check if an item exists in a string List and if it exists, get its index
- How to group by and order by a collection using linq?
- How to use Linq and lambda expressions on a Dictionary<string, List<T>> to a specify its order based on Key
- How to break link between source collection and LINQ result
- How to join two tables, and retrieve item collection using Linq
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- How to restore the quantity and consumption fields to their original numbers in the Item table after deleting an Order with Items
- C# Linq To Xml - How do Select collection of elements and modify each using lambda?
- how can I add a column to IQueryable object and modify its values
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- What does this C# code with an "arrow" mean and how is it called?
- Get previous and next item in a IEnumerable using LINQ
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How to get the item before current and after current in a dictionary with Linq / C#?
- How to union two data tables and order the result
- I am trying to learn how to bind an IEnumerable LINQ collection to a repeater
- How can I iterate over a collection and change values with LINQ extension methods?
- How to intercept and modify SQL query in Linq to SQL
- How do I convert IEnumerable to Collection with linq
- How does Visual Studio evaluate the IEnumerable without breaking into its IEnumerator<T>'s MoveNext?
- How to find and remove duplicate objects in a collection using LINQ?
- How do I LINQ order a collection
- How to order a list by property and group the elements in HTML Razor?
- How do I order the elements in a group by linq query, and pick the first?
- How to find duplicates in a List<T> quickly, and update the original collection
More Query from same tag
- C# LINQ/Lambda scope question
- Using EF to return specific formatted JSON from ASP.NET MVC
- Return first XElement in node with greatest number of fields
- Select all flagged values in enum using LINQ
- Custom LINQ-style implementation
- C# LINQ - Select statement that compares all properties of a class with a different instance of the same class?
- Can I force Automapper to initialise properties in a certain order?
- Entity Framework - dynamic sql
- Linq outer join with where, need help to select first
- Mix Items to new List with Linq
- Why predicate isn't filtering when building it via reflection
- LINQ: select first value of every minute in table
- How to merge trees (and sum counts) using LINQ?
- How to make null comparison to return false
- In C#, what is the best way to sort a list of objects by a string property and get correct order?
- How do I alter the contents of IDictionary using LINQ (C# 3.0)
- Why can't I use LINQ with WorkItemCollection
- LINQ query sometimes (randomly, for the exact same data) throwing NullReferenceException
- Merging two Query Results
- Query only the first element in C# ObjectQuery like LIMIT = 1 in SQL
- Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON
- how to filter data from from a dictionary to list
- Filter list of entity objects by string child object property using Linq Lambda
- Linq query where Contains() checks for list of strings
- Why is ReSharper telling me my expression is always true?
- How to do an update with Linq, Lambda and EF
- Linq - pulling back Parent when item in a Collection has a property that matches a string
- Group by and then select based on those group values
- How to create a dynamic LINQ query where clause
- nested linq queries, how to get distinct values?