排序和删除 C# 多维列表中的行

本文关键字:列表 删除 排序 | 更新日期: 2023-09-27 17:57:15

 private void button1_Click(object sender, EventArgs e)
    {
        var trackList = new List<Track>();
        trackList.Add(new Track{ TrackID = 1234, Name = "I'm Gonna Be (500 Miles)", Artist = "The Proclaimers(I)", Album = "Finest(I)", PlayCount = 10, SkipCount = 1 });
        trackList.Add(new Track { TrackID = 5678,Name = "I'm Gonna Be (1000 Miles)",Artist = "The Proclaimers(II)",Album = "Finest(II)", PlayCount = 20,SkipCount = 2});
        trackList.Add(new Track { TrackID = 9101,Name = "I'm Gonna Be (2000 Miles)",Artist = "The Proclaimers(III)",Album = "Finest(II)", PlayCount = 40,SkipCount = 3});
        trackList.Add(new Track { TrackID = 1213,Name = "I'm Gonna Be (4000 Miles)",Artist = "The Proclaimers(IV)",Album = "Finest(IV)", PlayCount = 80,SkipCount = 4});
        //How to sort based on column name:Album 
        //something like this below.
        trackList.Sort(trackList([0 4]);
        //How to remove rows after search items in a column based on some criteria
        trackList.RemoveAll(Album => Album.Contains("(II)"));
         //How to Populate resuts in a listbox
         //listBox1.DataSource = trackList();
    }
    public class Track
    {
        public int TrackID { get; set; }
        public string Name { get; set; }
        public string Artist { get; set; }
        public string Album { get; set; }
        public int PlayCount { get; set; }
        public int SkipCount { get; set; }
    }
  1. 如何根据列名称进行排序:相册

  2. 查找列中的项目并删除这些行。

  3. 结果应放在列表框中,也可以创建新列表。

排序和删除 C# 多维列表中的行

您可以使用 LINQ:

trackList = trackList
    .Where(t => !t.Album.Contains("(II)"))
    .OrderBy(t => t.Album)
    .ToList();

顺便说一句,这不是一个多维列表,而是一个List<Track>.