HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Using Linq instead of multiple foreach loops

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqloopsinsteadusingmultipleforeach

Problem

A project I'm working on often has lists within lists within lists etc. The original author often uses complicated linq expressions, but when adapting them I mostly get hopelessly bogged down and resort to foreach's which makes me feel like a lesser being (joke). With an expression such as the following, what would the equivalent Linq expression be, and would you bother taking the time to make it, instead of the 'easy' foreach option.

var participantList = new List();

        foreach (var community in communities)
        {
            foreach (var tournament in community.Tournaments)
            {
                foreach (var round in tournament.Rounds)
                {
                    foreach (var match in round.Matches)
                    {
                        if (match.Home.ParticipantId.IsNotNull())
                        {
                           participantList.Add(match.Home.ParticipantId); 
                        }
                        if (match.Away.ParticipantId.IsNotNull())
                        {
                           participantList.Add(match.Away.ParticipantId); 
                        }
                    }

                }
            }
        }

Solution

The LINQ would be:

var participantList = communities.SelectMany(c => c.Tournaments
                                                   .SelectMany(t => t.Rounds
                                                                     .SelectMany(r => r.Matches)))
                                  .Where(m => m.Home.ParticipantId.IsNotNull() ||
                                              m.Away.ParticipantId.IsNotNull())
                                  .ToList();


LINQ does not add much imo, if the logic was more complicated the for loops are nicer to debug.

One downside with LINQ for this is that it requires formatting to be readable. If you rename things the formatting needs to be maintained.

With the foreach loops you get formatting for free.

Edit:
As per @RobH's suggestion:

var participantList = communities.SelectMany(c => c.Tournaments)
                                 .SelectMany(t => t.Rounds)
                                 .SelectMany(r => r.Matches)
                                 .Where(m => m.Home.ParticipantId.IsNotNull() || 
                                             m.Away.ParticipantId.IsNotNull())
                                 .ToList();


Chaining is indeed nicer.

Code Snippets

var participantList = communities.SelectMany(c => c.Tournaments
                                                   .SelectMany(t => t.Rounds
                                                                     .SelectMany(r => r.Matches)))
                                  .Where(m => m.Home.ParticipantId.IsNotNull() ||
                                              m.Away.ParticipantId.IsNotNull())
                                  .ToList();
var participantList = communities.SelectMany(c => c.Tournaments)
                                 .SelectMany(t => t.Rounds)
                                 .SelectMany(r => r.Matches)
                                 .Where(m => m.Home.ParticipantId.IsNotNull() || 
                                             m.Away.ParticipantId.IsNotNull())
                                 .ToList();

Context

StackExchange Code Review Q#121895, answer score: 12

Revisions (0)

No revisions yet.