简体   繁体   English

如何在 C# 控制台应用程序中显示模型列表

[英]How to display a list of a model in a C# console application

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"我是 C# 的新手,我一直在努力做以下事情:我正在尝试在控制台应用程序中列出一个列表,我有一个名为“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 , 1,x的团队,
2,y's Team , 2,y的团队,
3,z's Team 3、z的团队

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 更改为:

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

I get this:我明白了:

TeamModel团队模型
TeamModel团队模型
TeamModel团队模型

You can achieve this by using您可以通过使用来实现这一点

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

Using Linq使用 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.您可以使用.SelectMany()展平嵌套列表,然后可以迭代展平列表,也可以将其转换为字符串列表然后打印。

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.将序列的每个元素投影到 IEnumerable 并将生成的序列展平为一个序列。

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

You may want to consider overriding the ToString() method for your TeamModel class;您可能需要考虑为您的 TeamModel 类覆盖 ToString()方法;

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:您可以测试 ToString 方法,如以下代码示例所示:

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

Is there a reason that TeamOne, TeamTwo and TeamThree are created as lists given that each "list" contains a single team?鉴于每个“列表”包含一个团队,是否有理由将 TeamOne、TeamTwo 和 TeamThree 创建为列表? 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:与您自己的解决方案的唯一区别是在您拥有的解决方案中增加了一个foreach

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}");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM