【问题标题】:What is the best way to display List items on the console based on the value of the items' properties?根据项目属性的值在控制台上显示列表项目的最佳方式是什么?
【发布时间】:2015-04-26 16:06:40
【问题描述】:

我有一个包含一些具有 2 个字符串和 2 个 int 属性的对象的列表。我希望能够根据第一个属性的内容显示对象的所有 4 个属性。 例如:我想显示列表中第一个属性为“Mozart”的所有项目的所有数据。 提前致谢!

我有一个非常基本的类,它有 4 个属性、2 个字符串和 2 个整数,它们都将各自的 getter/setter 设置为 public。

我还有一个包含其中一些对象的列表。

我的代码如下所示。

Console.WriteLine("Give in the name you want to search for!");
string s = Console.ReadLine();

在此之后,我想检查第一个属性是否为“s”,如果是,则在屏幕上显示该给定对象的所有数据。

【问题讨论】:

  • 您是否已经尝试过?使用循环 (foreach) 和条件运算符 (if) 实际上可能会派上用场。
  • 我还没有尝试任何东西,我对列表还很陌生,所以我真的不知道从哪里开始。
  • 您可以先在文档中阅读它们,然后尝试一些代码来解决您遇到的问题,最后如果您无法这样做,请在此处显示您的进度,以便我们提供帮助。
  • 我尝试了 foreach,但我无法访问我尝试洗牌的对象的属性。
  • 一种方法是在您的对象中,覆盖 ToString 方法以提供正确格式的字符串,然后只需遍历集合并打印出 object.ToString。在没有看到您的目标代码的情况下,我们不能推荐很多其他选项,因为您的属性/字段可能是私有的。提供您的代码和您想要在屏幕上显示的示例。

标签: c# list properties console items


【解决方案1】:

看看这个,如果你有任何困难,请告诉我:)

void Main()
{
    List<Music> myMusic = new List<Music>
    {
        new Music 
        { 
            Artist = "Mozart", 
            Album = "Mozarts amazing album", 
            TotalTracks = int.MaxValue, 
            Etc = int.MinValue
        },
        new Music 
        { 
            Artist = "Foo", 
            Album = "Bar", 
            TotalTracks = int.MaxValue, 
            Etc = int.MinValue
        },
    };


    var mozartsMusic = myMusic.Where(music => music.Artist == "Mozart")
                              .ToList();

    mozartsMusic.ForEach(Console.WriteLine);

}

public class Music
{
    public string Artist { get; set; }
    public string Album { get; set; }
    public int TotalTracks { get; set; }
    public int Etc { get; set; }

    public override string ToString()
    {
        return string.Join("\n",this.GetType().GetProperties().Select(p=>string.Format("{0} {1}", p.Name, p.GetValue(this))));
    }
}

【讨论】:

  • 是的,这也有效,我的错误是我试图在 foreach 中使用 obj 而不是 var。
【解决方案2】:

这样的事情可以解决问题:

class Datum
{
    public string Composer { get; set; }

    ///wharever other proerties you need

    public string DisplayOutput()
    {
        return this.Composer //+ however you want it displayed
    }
}


class Program
{
    static void Main(string[] args)
    {

        List<Datum> data = new List<Datum>();

        foreach (var outputLine in data.Where(d => d.Composer == "Mozart").Select(d=>d.DisplayOutput())
        {
            Console.WriteLine(outputLine);
        }

    }
}

【讨论】:

    【解决方案3】:

    这应该是一种可能的方式:

    class Composer
    {
       public Composer( string lastName, string firstName, int year, int month )
       {
          LastName = lastName;
          FirstName = firstName;
          YearOfBirth = year;
          MonthOfBirth = month;
       }
       public string LastName { get; set; }
       public string FirstName { get; set; }
       public int YearOfBirth { get; set; }
       public int MonthOfBirth { get; set; }
       public override string ToString()
       {
           return string.Format( "{0} {1} {2} {3}", LastName, FirstName, YearOfBirth.ToString(), MonthOfBirth.ToString() );
       }
    }
    
    
    class Program
    {
       private static new List<Composer> composerList = new List<Composer>();
    
       static void Main( string[] args )
       {
          composerList.Add( new Composer( "Mozart", "Wolfgang", 1756, 1 ) );
          composerList.Add( new Composer( "Vivaldi", "Antonio", 1678, 3 ) );
    
          Console.WriteLine( "Please enter a name you want to search for!" );
          string name = Console.ReadLine();
          ShowComposerData( name );
       }
    
    
       private static void ShowComposerData( string name )
       {
          foreach( Composer comp in composerList )
          {
             if( comp.LastName == name )
             {
                Console.WriteLine( comp.ToString() );
             }
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多