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
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();
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).
- How to modify an item in an IEnumerable collection and retain its order
- Replace current collection item in IEnumerable anonymous statement with result of statement
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- Updating an item property within IEnumerable but the property doesn't stay set?
- LINQ - selecting second item in IEnumerable
- Why doesn't the Controls collection provide all of the IEnumerable methods?
- Get previous and next item in a IEnumerable using LINQ
- Update item in IEnumerable
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- An item in IEnumerable does not equal an item in List
- Does FirstOrDefault return a reference to the item in the collection or the value?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- passing dynamic expression to order by in code first EF repository
- How to add an item in a collection using Linq and C#
- I am trying to learn how to bind an IEnumerable LINQ collection to a repeater
- LINQ WHERE method alters source collection
- How do I convert IEnumerable to Collection with linq
- Foreach on collection cast to IEnumerable work slower than without cast?
- How to order a collection and its subcollection using LINQ?
- Return best fit item from collection in C# 3.5 in just a line or two
- Divide a large IEnumerable into smaller IEnumerable of a fix amount of item
- Change item in collection with LINQ
- Most elegant way to process first IEnumerable item differently
- How do I LINQ order a collection
- Order parent collection by minimum values in child collection in Linq
- Why doesn't IOrderedEnumerable retain order after where filtering
- LINQ Source Code Available
- Find all items where child collection doesn't contain an item
- LINQ order a collection of objects by date in descending order
- asp.net linq query datatable
- Hash checking method in where clause MongoDB and LINQ
- C# LINQ SEO General programming question, how to correctly get the results of a location?
- The entity or complex type 'CreditFacility_Web.Models.CreditFacilityModel.SavingsAccount' cannot be constructed in a LINQ to Entities query
- Entity Framework VS pure Ado.Net
- How pass the right side "where" to an IQueryable instantiation in c#?
- How can I specify the range of rows my LINQ query should retrieve
- Linq filter parent objects based on child objects collection
- LINQ find records with no join
- List does not contain a definition for 'ConvertAll' and no extension method 'ConvertAll' accepting a first argument of type 'List< >'
- comparing datetime less than or equal to not working
- LINQ expression using a previously defined variable
- how do I pass null to int column in linq
- Replacing characters in a string array with linq
- How to get the next 12 months with empty months
- One context reference LINQ query throws multiple references exception - BUG?
- Linq for nested loop
- How to write Linq Queries for CRUD operations using Entity Framework with Asp.Net?
- C# Linq generic search
- LINQ query for retrieving items that contain any value from a list?