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
- Linq to SQL select multiple columns
- Compile ERROR: The type is defined in an assembly that is not referenced
- Multiple group by and Sum LINQ
- Get Most Recent Insert In LiteDB c# .net
- run Task as I type with linq(cancel previous Task if still running)
- How to sort in a linq query?
- Linq to XML query returining a list of strings
- C# Reactive Extensions (rx) FirstOrDefault enumerates entire collection
- Is there a way I could filter values using the Model class?
- In C#, what is the fastest way to remove blank entries in a string[] or List<string>?
- LINQ - Add to database does not show up
- How to get top most parent in self reference object
- Select and sum DataTable rows with criteria
- LINQ To SQL: Delete entity (by ID) with one query
- Convert regex matches to the list of strings
- DbExpressionBinding requires an input expression with a collection ResultType
- How to use a left outer join in a generic repository?
- Which LINQ expression is faster
- Update DataTable from another table with LINQ
- Store multi-type OrderBy expression as a property
- How do I return all order items for all orders?
- LinQ query group the same table twice
- Linq: Where count greater than value
- How to retrieve a binding to a queryable member?
- Linq Left Join Fails on List when right side is empty
- Assigning variable values to lambda expression to be passed to repository method
- Why can't I pass an anonymous type as a parameter to a function?
- jQuery Datatable with MVC 5 and Entity Framework
- Join the result of a left join to another table
- Convert this SQL Statement to LINQ