如何从嵌套的C#列表中删除重复值

时间:2013-02-18 22:05:24

标签: c# list linq-to-sql nested-lists

我的模型设置如下:

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'

1 个答案:

答案 0 :(得分: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
    }
}