Reputation: 1120
One of the great features of ReSharper in Visual Studio 2017 is that it will refactor my foreach loops into simple Linq expressions.
So it'll take:
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);
}
and make it:
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();
BUT, my question is, when it comes time to debug or test that statement. is there a way that I can still step through each item in the collection one by one like the foreach could?
I honestly don't know how because it's like the runtime just does the work of building the collection when it's a Linq Expression. So I guess the real question is how do you debug Linq Expressions?
Upvotes: 1
Views: 520
Reputation: 2405
The problem isn't with ForEach
- you can put a breakpoint in the middle, even in a single line statement. Just click there and press F9. ForEach
changes nothing.
The problem is that you're trying to debug in the middle of the object initializer syntactic sugar. This is not possible. Object initializers shouldn't be this complex.
See: https://stackoverflow.com/a/5528738/7866667
Upvotes: 1