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」とまったく同じであることがわかります。私がやろうとしているのは、これらの重複した値を持たないリストを取得することです。重複したレポート時間値の 1 つだけを保持します。したがって、上記に基づく私の理想的なフィルタリングされたリストは次のようになりますDayReportTimes

    [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インスタンスの数で線形時間のパフォーマンスが得られます。

もちろん、ハッシュセットを再利用して、一度に1日だけ実行することもできます。

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);
}

上記の例が十分に明確であることを願っています。また、冒頭で述べたように、必ずGetHashCodeとEqualsをReportTimeクラスに実装してください。次に例を示します。

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 に答える