我正在尝试获取非整数区间的小时列表。例如,如果间隔是 10 到 23,它应该返回 [1,2,3,4,5,6,7,8,9,24]。
我写了这个方法。
List<int> GetTimeList(List<Mail> Mails, int TransferSpeed)
{
List<int> Hours = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
foreach (Mail mail in Mails)
{
Hours.RemoveAll(x => x >= mail.DateAndTime.Hour && x <= GetRoundedTransferTime(TransferSpeed, mail.Size));
}
return Hours;
}
它实际上工作正常,但我正在寻找更优雅、更高效的方式。
如果你能帮我解决这个问题,我将不胜感激。
最佳答案
您可以使用 Linq 的 Except
。
像这样:
List<int> intervals=new List<int>
{
10,11,12,13,14,15,16,17,18,19,20,21,22,23
};
List<int> Hours = Enumerable.Range(1,24).ToList();
var NotInIntervals=Hours.Except(intervals).ToList();
foreach(var hour in NotInIntervals.OrderBy(x=>x))
{
Console.WriteLine(hour);
}
结果
1,2,3,4,5,6,7,8,9,24
关于c# - 获取不在间隔内的小时列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56203289/