Which code segment should you insert at line 02?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. You create the classes shown in the following exhibit:

You add the following code segment to the application. (Line numbers are included for reference only.)

01 public void QueryPlayers (List <League> leagues) {
02 …
03 }

You create a LINQ query to retrieve a collection of Player objects.
You need to ensure that the collection includes all the players from each team and every league. Which code segment should you insert at line 02?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. You create the classes shown in the following exhibit:

You add the following code segment to the application. (Line numbers are included for reference only.)

01 public void QueryPlayers (List <League> leagues) {
02 …
03 }

You create a LINQ query to retrieve a collection of Player objects.
You need to ensure that the collection includes all the players from each team and every league. Which code segment should you insert at line 02?

A.
var query = leagues.Select(l => l.Teams.Select(t => t.Players));

B.
var query = leagues.Select(l => l.Teams.SelectMany(t => t.Players));

C.
var query = leagues.SelectMany(l => l.Teams.SelectMany(t => t.Players));

D.
var query = leagues.SelectMany(l => l.Teams.Select(t => t.Players));



Leave a Reply 1

Your email address will not be published. Required fields are marked *


John Galt

John Galt

Verified the answer using Visual Studio. C is the only one that compiles and doesn’t throw an exception during runtime:

public class League
{
public string LeagueName { get; set; }
public List Teams = new List();
}

public class Team
{
public string TeamName { get; set; }
public List Players = new List();
}

public class Player
{
public string FullName { get; set; }
}

void main()
{
Player player1 = new Player() { FullName = “Player1” };
Player player2 = new Player() { FullName = “Player2” };
Player player3 = new Player() { FullName = “Player3” };
Player player4 = new Player() { FullName = “Player4” };
Player player5 = new Player() { FullName = “Player5” };
Player player6 = new Player() { FullName = “Player6” };
Player player7 = new Player() { FullName = “Player7” };
Player player8 = new Player() { FullName = “Player8” };

Team team1 = new Team() { TeamName = “Team1” };
team1.Players.Add(player1);
team1.Players.Add(player2);
Team team2 = new Team() { TeamName = “Team2” };
team2.Players.Add(player3);
team2.Players.Add(player4);
Team team3 = new Team() { TeamName = “Team3” };
team3.Players.Add(player5);
team3.Players.Add(player6);
Team team4 = new Team() { TeamName = “Team4” };
team3.Players.Add(player7);
team3.Players.Add(player8);

League league1 = new League() { LeagueName = “League1” };
league1.Teams.Add(team1);
league1.Teams.Add(team2);

League league2 = new League() { LeagueName = “League2” };
league2.Teams.Add(team3);
league2.Teams.Add(team4);

List leagues = new List();
leagues.Add(league1);
leagues.Add(league2);

var query = leagues.SelectMany(l => l.Teams.SelectMany(t => t.Players));

foreach (Player player in query)
Debug.WriteLine(player.FullName);
}

OUTPUT:
Player1
Player2
Player3
Player4
Player5
Player6
Player7
Player8