C# 如何从嵌套的C列表中删除重复值
我的模型设置如下所示:C# 如何从嵌套的C列表中删除重复值,c#,list,linq-to-sql,nested-lists,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; s
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; }
}
重要信息:请确保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
}
}
我建议使用。我以前从未使用过哈希集,是否可以从我上面提供的信息中快速创建一个示例,或者创建一个与我的数据类似的链接。请参阅编辑;请务必阅读有关哈希集的内容!除非您了解它的作用以及为什么有用,否则无法有效地使用它。哈希集类似于字典,在字典中,键和值之间没有区别。它们的行为很像数学集合。多次添加项目只会添加一次,不会产生错误。
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);
}
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
}
}