1

我有一个如下模型设置:

public class ReportScheduleModel
{
    public string Day { get; set; }
    public List<ReportTimes> reportTimes { get; set; }
}

public class ReportTimes
{
    public byte hourOfDay { get; set; }
    public byte minuteOfDay { get; set; }
    public string reportType { get; set; }
}

然后我可以使用以下列表格式将整个列表传递回我的控制器:

List<ReportScheduleModel> ReportSchedule
    [0]->Day: 'Sunday'
         [ReportTimes]: [0]->hourOfDay: '09'
                        minuteOfDay: '23'
                        reportType: 'Test1'
                   [1]->hourOfDay: '08'
                        minuteOfDay: '11'
                        reportType: 'Test2'
    [1]->Day: 'Sunday'
         [ReportTimes]: [0]->hourOfDay: '09'
                        minuteOfDay: '23'
                        reportType: 'Test1'
                        [1]->hourOfDay: '11'
                        minuteOfDay: '30'
                        reportType: 'Test1'
    [2]->Day: 'Monday'
         [ReportTimes]: [0]->hourOfDay: '09'
                        minuteOfDay: '23'
                        reportType: 'Test1'

在上面的列表中,您注意到ReportSchedule[0]两者ReportSchedule[1]的报告时间完全相同,即“09:23 Test1”。我想要做的是得到一个没有这些重复值的列表,它只保留一个重复的报告时间值。因此,我基于上述内容的理想过滤列表将是:(Day不分组/唯一无关紧要,只是ReportTimes基于相同的“日”)

    [0]->Day: 'Sunday'
             [ReportTimes]: [0]->hourOfDay: '09'
                            minuteOfDay: '23'
                            reportType: 'Test1'
                       [1]->hourOfDay: '08'
                            minuteOfDay: '11'
                            reportType: 'Test2'
        [1]->Day: 'Sunday'
             [ReportTimes]: [0]->hourOfDay: '11'
                            minuteOfDay: '30'
                            reportType: 'Test1'
        [2]->Day: 'Monday'
             [ReportTimes]: [0]->hourOfDay: '09'
                            minuteOfDay: '23'
                            reportType: 'Test1'
4

1 回答 1

0

重要事项确保您的ReportTimes类以合理的方式实现 GetHashCode 和 Equals:即相同的条目总是散列到相同的值,而不同的时间条目散列到不同的值。

然后,您可以为每一天创建一个 HashSet 数据结构,并线性遍历您的嵌套列表,将所有列表中的所有报告时间添加到按天计算的适当集合中。

以上将确保每天只保留唯一的 ReportTimes 实例。并且它将在 ReportTimes 实例的数量上具有线性时间性能!

当然,您也可以重复使用哈希集,一次只做一天..

var sets  = new Dictionary<string, HashSet<ReportTimes>>();

// assuming ReportSchedule is the list of ReportScheduleModel items
foreach(var scheduleItem in ReportSchedule)
{
     if(!sets.ContainsKey(scheduleItem.Day))
        sets.Add(scheduleItem.Day, new HashSet<ReportTimes>());

     foreach(var rt in scheduleItem.reportTimes)
     {
          sets[scheduleItem.Day].Add(rt);
     }
}

// at this point, each set in the sets dictionary will contain only unique items
// if you wanted to get all the unique report times for Sunday you would use something like:
foreach(var rt in sets["Sunday"])
{
    Console.WriteLine("{0}:{1} - {2}", rt.hourOfDay, rt.minuteOfDay, rt.reportType);
}

我希望上面的例子足够清楚。正如我在开始时所说,请确保在 ReportTime 类中实现 GetHashCode 和 Equals。这是一个例子:

public class ReportTimes
{
    public byte hourOfDay { get; set; }
    public byte minuteOfDay { get; set; }
    public string reportType { get; set; }

    public override int GetHashCode()
    {            
        return reportType.GetHashCode ^ (hourOfDay << 8) ^ (minuteOfDay);
    }

    public override bool Equals(object other)
    {
        if(other is ReportTimes)
        {
            var ort = (ReportTimes)other;

            // returns true if the 'other' object represents the same time & type
            return    ort.hourOfDay.Equals(hourOfDay);
                   && ort.minuteOfDay.Equals(minuteOfDay);
                   && ort.reportType.Equals(reportType);
        }

        return false;  // if comparing to a non-ReportTimes object always return false
    }
}
于 2013-02-18T22:22:40.153 回答