2013-02-18 2 views
1

중첩 된 C 번호 목록에서 중복 값 제거하는 방법 :내가 모델 설정을 아래와 같이이

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' 
: 나는 다음 목록 형식으로 내 컨트롤러에 다시 전체 목록을 전달할 수 있습니다

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

위의 목록에서 ReportSchedule[0]ReportSchedule[1]은 모두보고 시간이 "09:23 Test1"과 똑같다는 것을 알 수 있습니다. 내가하려고하는 것은 이러한 중복 값이없는 목록에 도달하는 것입니다. 중복보고 시간 값 중 하나만 유지합니다. 따라서, 나의 이상적인 필터링 된 목록을 기준으로 위의 것 :

[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

[HashSet의 ] (http://msdn.microsoft.com/en-us/library/bb359438.aspx) : 다음은 예입니다. –

답변

0

중요 확인 (그냥 같은 '일'을 기준으로 ReportTimesDay이/고유의 그룹화되지 않는다는 문제가되지 않습니다) 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); 
} 

나는 위의 예는 충분히 명확 바랍니다 .. 또한 해시 세트를 다시 사용하고 단지 한 번에 하나의 하루에 할 수 있습니다. 그리고 처음에 말했듯이 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 
    } 
} 
+0

전에 해시 세트를 사용한 적이 없습니다. 위에서 제공 한 정보의 간단한 예 또는 내 데이터와 유사한 링크를 사용했을 것입니다. – GPB

+0

수정 사항보기; 해시 세트에 대해 읽어보십시오! 그것이 무엇이고 왜 유용한 지에 대한 이해를 개발하지 않는 한 효과적으로 사용할 수 없습니다. –

+0

해시는 키와 값을 구별하지 않는 사전과 유사합니다. 그들은 수학적 세트처럼 행동합니다. 항목을 여러 번 추가하면 오류가 발생하지 않고 항목이 한 번만 추가됩니다. –

관련 문제