How to remove duplicate values from nested C# list -
i have model setup below:
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; } }
i can pass entire list controller next list format:
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'
in list above notice reportschedule[0]
, reportschedule[1]
both have exact same reporting times listed of "09:23 test1". i'm trying list not have these duplicate values, keeps 1 of duplicate reporting time values. therefore, ideal filtered list based on above be: (it doesn't matter day
not grouped/unique, reporttimes
based on same 'day')
[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'
important create sure reporttimes class implements gethashcode , equals in sensible way: i.e. same entry hashes same value, , different time entries hash different values.
then can create hashset info construction each day , linearly traverse nested lists adding study times lists appropriate set day.
the above ensure unique reporttimes instances kept each day. , have linear-time performance in number of reporttimes instances!
of course, re-use hash set , 1 day @ time..
var sets = new dictionary<string, hashset<reporttimes>>(); // assuming reportschedule 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); } } // @ point, each set in sets dictionary contain unique items // if wanted unique study times sunday utilize like: foreach(var rt in sets["sunday"]) { console.writeline("{0}:{1} - {2}", rt.hourofday, rt.minuteofday, rt.reporttype); }
i hope illustration above clear enough. , said in beginning, please create sure implement gethashcode , equals in reporttime class. here's example:
public class reporttimes { public byte hourofday { get; set; } public byte minuteofday { get; set; } public string reporttype { get; set; } public override int gethashcode() { homecoming reporttype.gethashcode ^ (hourofday << 8) ^ (minuteofday); } public override bool equals(object other) { if(other reporttimes) { var ort = (reporttimes)other; // returns true if 'other' object represents same time & type homecoming ort.hourofday.equals(hourofday); && ort.minuteofday.equals(minuteofday); && ort.reporttype.equals(reporttype); } homecoming false; // if comparing non-reporttimes object homecoming false } }
c# list linq-to-sql nested-lists
No comments:
Post a Comment