score:1
first of all, a list inside a foreach is immutable, you can't add or delete content, nor rewrite an element. there are a few ways you could handle this situation:
groupby
this is the method i would use. you can group your list by the property you want, and iterate through the igrouping formed this way
var groups = list.groupby(x => x.yourproperty);
foreach(var group in groups)
{
//your code
}
distinct properties list
you could also save properties in another list, and cycle through that list instead of the original one
var propslist = list.select(x=>x.yourproperty).distinct();
foreach(var prop in propslist)
{
var tmplist = list.where(x=>x.yourproperty == prop);
//your code
}
while loop
this will actually do what you originally wanted, but performances may not be optimal
while(list.any())
{
var prop = list.first().yourproperty;
var tmplist = list.where(x=>x.yourproperty == prop);
//your code
list.removeall(x=>x.yourproperty == prop);
}
score:3
first off, using foreach
in your example isn't a great idea for these reasons.
you're right to think there are performance downsides to iterating through the full list for each remaining someid
, but even making the list smaller every time would still require another full iteration of that subset (if it even worked).
as was pointed out in the comments, groupby
on someid
organizes the elements into groupings for you, and allows you to efficiently step through each subset for a given someid
, like so:
_mylist.groupby(x => x.someid)
.select(g => dosomethingwithgroupedelements(g));
jon skeet has an excellent set of articles about how the linq extensions could be implemented. i highly recommend checking it out for a better understanding of why this would be more efficient.
Source: stackoverflow.com
Related Query
- Removing from a collection while in a foreach with linq
- Find a certain value from a source collection in a target collection of different type with linq
- Linq to update a collection with values from another collection?
- Removing characters from strings with LINQ
- Joining two tables with LINQ while also returning null records from the second table
- Select only subclasses from collection with Linq
- C# Linq query help removing foreach loops creating cleaner code
- How to select distinct employees with LINQ based on ID from an employee collection where employee 's salary is 2nd highest?
- creating Linq to sqlite dbml from DbLinq source code
- LINQ for removing elements that are started with other element from list
- Removing specific values from a Dictionary C# with Linq
- Get objects from collection with Linq
- Linq to objects - Search collection with value from other collection
- Can't add a new record with an integer value into database by using linq from code C#
- LINQ + join with nested foreach razor output writing out title lines from groupby
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- How to write a LINQ query to select from a collection with given set of matching IDs
- Could i mix values from local data with values returned from database while using LINQ to SQL?
- How can I add and remove records from a collection with LINQ
- Error while removing item from collection
- Select all fields from collection with related items in another collection using LINQ
- Select maximum version from collection with LINQ in EF
- Linq Concating a list of tags with count from an inner collection of an outer collection
- Convert the code from Foreach loop to Linq
- Rewrite nested foreach with linq if there are some operations apart from internal foreach loops
- c# Linq or code to extract groups from a single list of source data
- Entity Framework dynamic linq where from generic source with dynamic where clause
- Data paging in linq without removing records from the data source
- How to select only those records from collection which have relation with another table using Linq
- Create a list from two object lists with linq
More Query from same tag
- LINQ join multi tables in Lambda ex
- Linq Expression tree Any() issue
- How do we perform groupby operation on data fetched from a database
- Can't get the language entity with the method _languageRepository.FirstOrDefault(x=>x.Name=langCode)
- Is recursive query possible in LINQ to Entities
- SubSonic 3 and Linq Parameterized Queries
- Paging a Gridview with Linq
- Foreach delete with linq
- Flatten A Collection With Linq Query
- Explain Eagar loading and Lazy loading in NHibernate
- How to optimize SQL query generated by Entity Framework in SQL Server Management Studio?
- EF Core dynamic filter
- GroupBy to remove duplicates from IEnumerable list of objects
- Linq if DateTime field is older than X hours
- Sum and Group multiple Linq Lists
- Compare two datatables to find matching values
- Determining the scope of LINQ statement with a using around the DB context
- Selecting XML nodes using LINQ
- How to get and use source of grouping in LINQ?
- LINQ extract dataTable information
- linq group by multipe aggregates
- Use LINQ to flatten tree getting deepest value
- How to chop up a string with LINQ
- Splitting a deferred IEnumerable<T> into two sequences without re-evaluation?
- Linq namespace issue
- Will indexing a SQL table improve the performance of a LINQ statement?
- How to convert DateTime to TimeSpan in Entity Framework query
- C# - LINQ - Performances between 2 style of query
- Files in directory sort by fileName ascending
- Building expression tree dynamically with typecasting