Home > Net >  How to display a list of a model in a C# console application
How to display a list of a model in a C# console application

Time:05-26

I am new to C# and I have been struggling to do the following: I'm trying to List a list in a console application, I have a model called "TeamModel"

public class TeamModel
{
    public int Id { get; set; }
    public string TeamName { get; set; }
    public List<PersonModel> TeamMembers { get; set; } = new List<PersonModel>();

    public TeamModel()
    {

    }
}

In my main class I have the following:

class Program 
{
    static void Main(string[] args)
    {
        List<TeamModel> TeamOne   = new List<TeamModel>(){new TeamModel() { Id =1, TeamName = "x's Team", TeamMembers  = null}};
        List<TeamModel> TeamTwo   = new List<TeamModel>(){new TeamModel() { Id =2, TeamName = "y's Team", TeamMembers  = null}};
        List<TeamModel> TeamThree = new List<TeamModel>(){new TeamModel() { Id =3, TeamName = "z's Team", TeamMembers  = null}};

        List<List<TeamModel>> listOfTeams = new List<List<TeamModel>> (){TeamOne,TeamTwo,TeamThree};

        foreach (List<TeamModel> list in listOfTeams) 
        {
            Console.WriteLine(list);
        }
    }
}

Now when I run the program I expect the result to be:

1,x's Team ,
2,y's Team ,
3,z's Team

Instead what I'm getting is

System.Collections.Generic.List`1[TeamModel]  
System.Collections.Generic.List`1[TeamModel]  
System.Collections.Generic.List`1[TeamModel]

If I change the foreach to :

foreach (List<TeamModel> list in listOfTeams) 
{
    Console.WriteLine(String.Join(", ", list));
}

I get this:

TeamModel
TeamModel
TeamModel

CodePudding user response:

You can achieve this by using

foreach (List<TeamModel> list in listOfTeams) 
{
  foreach (var team in list)
  {
     Console.WriteLine(team.Id   " "   team.Name);
  }
} 

CodePudding user response:

Using Linq

You can Flatten the nested list using .SelectMany() and then either you can iterate over flatten list or you can convert it into List of string and then print it.

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

var strTeamDetails =  listOfTeams
         .SelectMany(teams => teams)  //Flatten the List.
         .Select(individualTeam => $"{individualTeam.Id} {individualTeam.Name}");  //Convert to IEnumearable of string

Console.WriteLine(string.Join(",\n", strTeamDetails)); //Print

CodePudding user response:

You may want to consider overriding the ToString() method for your TeamModel class;

From the link above;

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return "Person: "   Name   " "   Age;
    }
}

You can test the ToString method as shown in the following code example:

Person person = new Person { Name = "John", Age = 12 };
Console.WriteLine(person);
// Output:
// Person: John 12

CodePudding user response:

Is there a reason that TeamOne, TeamTwo and TeamThree are created as lists given that each "list" contains a single team? The following implementation would achieve what you stated was the expected output:

static void Main(string[] args)
{
    TeamModel TeamOne = new TeamModel { Id = 1, TeamName = "x's Team", TeamMembers = null };
    TeamModel TeamTwo = new TeamModel { Id = 2, TeamName = "y's Team", TeamMembers = null };
    TeamModel TeamThree = new TeamModel { Id = 3, TeamName = "z's Team", TeamMembers = null };

    List<TeamModel> listOfTeams = new List<TeamModel> { TeamOne, TeamTwo, TeamThree };

    foreach (TeamModel team in listOfTeams)
    {
         Console.WriteLine($"{team.Id}. {team.TeamName}");
    }
}

If you genuinely need a list of lists then the following implementation will work. The only difference from your own solution is an additional foreach inside the one you had:

static void Main(string[] args)
{
    List<TeamModel> TeamOne = new List<TeamModel>() { new TeamModel() { Id = 1, TeamName = "x's Team", TeamMembers = null } };
    List<TeamModel> TeamTwo = new List<TeamModel>() { new TeamModel() { Id = 2, TeamName = "y's Team", TeamMembers = null } };
    List<TeamModel> TeamThree = new List<TeamModel>() { new TeamModel() { Id = 3, TeamName = "z's Team", TeamMembers = null } };

    List<List<TeamModel>> listOfTeams = new List<List<TeamModel>>() { TeamOne, TeamTwo, TeamThree };

    foreach (List<TeamModel> list in listOfTeams)
    {
        foreach (TeamModel team in list)
        {
            Console.WriteLine($"{team.Id}. {team.TeamName}");
        }
    }
}
  •  Tags:  
  • c#
  • Related