【问题标题】:Sort multi-level list in C# where the inner layer contains a list of typed objects在 C# 中排序多级列表,其中内层包含类型化对象的列表
【发布时间】:2015-01-22 09:55:40
【问题描述】:

我有一个 List 包含多个 Lists 本身包含类型对象:

List<List<Activity>> SortActivities

Activity 类型的对象如下所示:

public class Activity
{
    public DateTime Date { get; set; }
    public string ProjectnumberUnformatted { get; set; }
    public int Year 
    { 
        get
        {
            return Date.Year;
        }
    }
    public int Month
    { 
        get
        {
            return Date.Month;
        }
    }
    public string MonthYear 
    { 
        get
        {
            return Month + " / " + Year;
        }
    }
    public string Customer 
    {
        get 
        {
            return ProjectnumberUnformatted.Split('/')[0];            
        } 
    }
    public string ProjectnumerFormatted 
    { 
        get
        {
            return ProjectnumberUnformatted.Split('/')[2];
        }      
    }
    public string MonthString 
    {
        get
        {
            switch (Month)
            {
                case 1:
                        return "Januar";
                case 2:
                        return "Februar";
                case 3:
                        return "März";
                case 4:
                        return "April";
                case 5:
                        return "Mai";
                case 6:
                        return "Juni";
                case 7:
                        return "Juli";
                case 8:
                        return "August";
                case 9:
                        return "September";
                case 10:
                        return "Oktober";
                case 11:
                        return "November";
                case 12:
                        return "Dezember";
                default:
                        return "Invalid";
            }
        }
    }
    public string Start { get; set; }
    public string Pause { get; set; }
    public string End { get; set; }
    public string Description { get; set; }
    public string Comment { get; set; }
}

在程序启动时,我有一个对象List&lt;Activity&gt;,其中包含未排序的活动。但是,每个活动都应按其项目编号排序。所以我需要实现一个方法,将List&lt;Activity&gt; 对象中的每个活动排序到它自己的List&lt;Activity&gt; 中,并将项目编号作为排序参数。然后将生成的列表放入List&lt;List&lt;Activity&gt;&gt;

如果可能的话,我只需要一个基本流程图,并使用一个简单的算法来完成这项工作,因为我真的不知道如何开始。

【问题讨论】:

  • 我认为分阶段进行:移至 Dictionary(Dictionary>) 而不是重新考虑您的问题,因为它太复杂,无法提供简单的可重复使用的答案,稍后尝试 LINQ 来安排它跨度>

标签: c# sorting multi-level


【解决方案1】:

听起来您实际上想要一个字典,其中键是项目编号,值是活动列表。听起来是 LINQ 的理想工作:

List<Activity> activities = new List<Activity>
{
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "def" },
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "def" },
    new Activity { ProjectnumberUnformatted = "ghi" },
    new Activity { ProjectnumberUnformatted = "def" },
};

Dictionary<string, List<Activity>> activitiesKeyedOnProjectNumber = (
    from activity in activities
    group activity by activity.ProjectnumberUnformatted into grouped
    select new { key = grouped.Key, value = grouped.ToList() }
    ).ToDictionary(
        keySelector: x => x.key,
        elementSelector: x => x.value
        );

【讨论】:

    【解决方案2】:

    您不想对对象进行排序,而是想对它们进行分组。 您需要做的是通过集合中的所有元素并将它们中的每一个分配给具有正确标识符的组。 在这种情况下,Dictionary&lt;key,List&lt;object&gt;&gt; 将是更好的数据结构选择。

    Dictionary<string, List<ISortableObject>> Sort(List<ISortableObject> items)
    {
      var result = new Dictionary<string, List<ISortableObject>>();
      foreach(var item in items)
      {
        if (!result.ContainsKey(item.SortingKey))
        { result[item.SortingKey]=new List<ISortableObject>(); }
        result[item.SortingKey].Add(item);
      }
    }
    

    【讨论】:

      【解决方案3】:

      好的,感谢您的所有回答。我稍微研究了一下 LINQ,想出了一个非常简单的语句来达到我的目的:

      var groupedActivities = activities.GroupBy(p => p.ProjectnumberUnformatted);
      

      这样,我得到一个枚举,其中包含其他类型的Activity 对象枚举,然后可以遍历这些子枚举并做我需要做的事情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2017-07-21
        • 1970-01-01
        相关资源
        最近更新 更多