Visual Studio 2017中ReSharper的一个重要特性是它将我的foreach循环重构为简单的Linq表达式。
所以它需要:
foreach (var windForecastDataRecord in good)
{
var dbRec = new WindDayAheadHourForecast
{
SyncId = currentSyncJobId,
Site = windForecastDataRecord.SITE,
PredictionTimeEst = string.IsNullOrEmpty(windForecastDataRecord.PREDICTIONTIME)
? (DateTime?) null: DateTime.Parse(windForecastDataRecord.PREDICTIONTIME),
TimeEst = string.IsNullOrEmpty(windForecastDataRecord.TIME)
? (DateTime?)null : DateTime.Parse(windForecastDataRecord.TIME),
MegaWatts = decimal.Parse(windForecastDataRecord.MW),
MaxiumOutput = decimal.Parse(windForecastDataRecord.MAXIMUMOUTPUT),
Flags = windForecastDataRecord.FLAGS,
Grp = windForecastDataRecord.GROUP,
Region = windForecastDataRecord.REGION,
Zone = windForecastDataRecord.ZONE
};
dbRecords.Add(dbRec);
}
并成功:
var dbRecords = good.Select(windForecastDataRecord => new WindDayAheadHourForecast
{
SyncId = currentSyncJobId,
Site = windForecastDataRecord.SITE,
PredictionTimeEst = string.IsNullOrEmpty(windForecastDataRecord.PREDICTIONTIME)
? (DateTime?) null
: DateTime.Parse(windForecastDataRecord.PREDICTIONTIME),
TimeEst = string.IsNullOrEmpty(windForecastDataRecord.TIME)
? (DateTime?) null
: DateTime.Parse(windForecastDataRecord.TIME),
MegaWatts = decimal.Parse(windForecastDataRecord.MW),
MaxiumOutput = decimal.Parse(windForecastDataRecord.MAXIMUMOUTPUT),
Flags = windForecastDataRecord.FLAGS,
Grp = windForecastDataRecord.GROUP,
Region = windForecastDataRecord.REGION,
Zone = windForecastDataRecord.ZONE
})
.ToList();
但是,我的问题是,当需要调试或测试该语句时。有没有办法,我仍然可以像foreach一样逐个逐步浏览集合中的每个项目?
老实说,我不知道怎么样,因为它就像运行时只是在Linq Expression中构建集合的工作。所以我想真正的问题是你如何调试Linq表达式?
答案 0 :(得分:1)
问题不在ForEach
- 您可以在中间放置断点,即使在单行语句中也是如此。只需点击那里然后按F9即可。 ForEach
没有任何改变。
问题在于您尝试在对象初始化程序语法糖的中间进行调试。这是不可能的。对象初始化器不应该是这么复杂。