C#C
C#3y ago
gr8stalin

❔ best practices for updating objects using GroupBy

i've got a collection of data objects. these data objects are related by domain-specific ids, and what I want to do is group them so i can update a property on every other group.

i've got the following set up so far, and it works:

var group = this.results.GroupBy(x => x.DomainSpecificId).Where((i, idx) => idx % 2 ==0);

foreach (var groupedData in group)
{
    foreach (var data in groupedData)
    {
        data.PropertyToUpdate = true;
    }
}


however, anyone who has read anything introductory about performance might feel as uneasy as i do about a loop nested within a loop.

am i worrying about the performance aspect of this too much, or am i missing a better way to do this? it looks like a lot of the top StackOverflow answers about this just use the nested loops to perform the iteration.
Was this page helpful?