我有这个方法
public static Status BiggestOverdue(List<StatusOverdueMinutes> overdueMinutes, DateTime lastReceived, int receiveTimeSpanMinutes)
{
var maxMinutesOverdueFound = 0;
var status = Status.Ok;
if (lastReceived.AddMinutes(receiveTimeSpanMinutes) >= DateTime.Now ||
receiveTimeSpanMinutes == -1)
return Status.Ok;
foreach (var overdueStatus in overdueMinutes.Where(overdueStatus =>
lastReceived.AddMinutes(receiveTimeSpanMinutes +
overdueStatus.OverdueMinutes) < DateTime.Now))
if (overdueStatus.OverdueMinutes > maxMinutesOverdueFound)
{
maxMinutesOverdueFound = overdueStatus.OverdueMinutes;
status = overdueStatus.StatusId;
}
return status;
}
状态枚举
public enum Status
{
Error = 0,
Warning = 1,
Information = 2,
Ok = 3,
NeedsConfig = 4,
Maintenance = 5,
Disabled = 6,
Enabled = 7
}
我有单元测试备份,它按预期工作(即从列表中返回最过期的状态)
public void ShouldReturnError()
{
var lastReceived = DateTime.Now.AddMinutes(-30);
var overdueMinutes = new List<StatusOverdueMinutes>
{
new StatusOverdueMinutes {OverdueMinutes = 10, StatusId = Status.Error},
new StatusOverdueMinutes {OverdueMinutes = 7, StatusId = Status.Warning},
new StatusOverdueMinutes {OverdueMinutes = 5, StatusId = Status.Information}
};
const int receiveTimeSpan = 15;
var status = CacheHelper.BiggestOverdue(overdueMinutes, lastReceived, receiveTimeSpan);
Assert.AreEqual(Status.Error, status);
}
[TestMethod]
public void ShouldReturnWarning()
{
var lastReceived = DateTime.Now.AddMinutes(-30);
var overdueMinutes = new List<StatusOverdueMinutes>
{
new StatusOverdueMinutes {OverdueMinutes = 20, StatusId = Status.Error},
new StatusOverdueMinutes {OverdueMinutes = 7, StatusId = Status.Warning},
new StatusOverdueMinutes {OverdueMinutes = 5, StatusId = Status.Information}
};
const int receiveTimeSpan = 15;
var status = CacheHelper.BiggestOverdue(overdueMinutes, lastReceived, receiveTimeSpan);
Assert.AreEqual(Status.Warning, status);
}
[TestMethod]
public void ShouldReturnInformation()
{
var lastReceived = DateTime.Now.AddMinutes(-30);
var overdueMinutes = new List<StatusOverdueMinutes>
{
new StatusOverdueMinutes {OverdueMinutes = 30, StatusId = Status.Error},
new StatusOverdueMinutes {OverdueMinutes = 27, StatusId = Status.Warning},
new StatusOverdueMinutes {OverdueMinutes = 10, StatusId = Status.Information}
};
const int receiveTimeSpan = 15;
var status = CacheHelper.BiggestOverdue(overdueMinutes, lastReceived, receiveTimeSpan);
Assert.AreEqual(Status.Information, status);
}
[TestMethod]
public void ShouldReturnOk()
{
var lastReceived = DateTime.Now.AddMinutes(-10);
var overdueMinutes = new List<StatusOverdueMinutes>
{
new StatusOverdueMinutes {OverdueMinutes = 20, StatusId = Status.Error},
new StatusOverdueMinutes {OverdueMinutes = 15, StatusId = Status.Warning},
};
const int receiveTimeSpan = 15;
var status = CacheHelper.BiggestOverdue(overdueMinutes, lastReceived, receiveTimeSpan);
Assert.AreEqual(Status.Ok, status);
}
Resharper建议循环体的一部分可以转换为linq。这就是它的建议:
foreach (var overdueStatus in overdueMinutes.Where(overdueStatus => lastReceived.AddMinutes(receiveTimeSpanMinutes + overdueStatus.OverdueMinutes) < DateTime.Now)
.Where(overdueStatus => overdueStatus.OverdueMinutes > maxMinutesOverdueFound))
{
maxMinutesOverdueFound = overdueStatus.OverdueMinutes;
status = overdueStatus.StatusId;
}
结果它说了这句话:
maxMinutesOverdueFound = overdueStatus.OverdueMinutes;
分配的值未在任何执行路径中使用,可以将其删除。所以我删除它。这使我的循环体成为:
foreach (var overdueStatus in overdueMinutes.Where(overdueStatus => lastReceived.AddMinutes(receiveTimeSpanMinutes + overdueStatus.OverdueMinutes) < DateTime.Now)
.Where(overdueStatus => overdueStatus.OverdueMinutes > maxMinutesOverdueFound))
{
status = overdueStatus.StatusId;
}
现在我的测试失败了。这是resharper中的一个错误吗?或者我在这里做一些完全愚蠢的事情。
答案 0 :(得分:3)
是的,resharper不了解您的代码并提出错误的建议。您需要maxMinutesOverdueFound
才能找到最大值,并在if
中连续更新此值:
if (overdueStatus.OverdueMinutes > maxMinutesOverdueFound)
{
maxMinutesOverdueFound = overdueStatus.OverdueMinutes;
status = overdueStatus.StatusId;
}
如果resharper现在尝试删除此if
并仅与初始值进行比较,则表示不正确。
因此,即使您要更新foreach中的值,LINQ转换resharper建议也是错误的,因为Where
仅按初始值过滤。
但是您可以简化整个查询,不需要foreach
或跟踪变量:
StatusOverdueMinutes maxOverdueStatus = overdueMinutes
.Where(odm => lastReceived.AddMinutes(receiveTimeSpanMinutes + odm.OverdueMinutes) < DateTime.Now)
.OrderByDescending(odm => odm.OverdueMinutes)
.FirstOrDefault();
if (maxOverdueStatus == null) return Status.Ok;
return maxOverdueStatus.StatusId;