1

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?

imsome1
  • 1,182
  • 4
  • 22
  • 37
Paul Duer
  • 1,100
  • 1
  • 13
  • 32
  • 2
    Put a breakpoint on the `SyncId = currentSyncJobId,` line and give it a try. – mjwills Oct 26 '17 at 12:20
  • Hrmm, it only lets me break point the WHOLE body of the NEW, is that what you mean? – Paul Duer Oct 26 '17 at 12:24
  • In my old code I could break in each and every iteration through the items in the collection I was foreaching over. So if the 5th item had something bad, I could stop four in and then look at what the 5th came through as. – Paul Duer Oct 26 '17 at 12:28
  • I did try it, yes. I have a date parse bug, let me fix that and see if I get what I want. – Paul Duer Oct 26 '17 at 12:29
  • Yes! That does what I want! – Paul Duer Oct 26 '17 at 12:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157570/discussion-between-paul-duer-and-mjwills). – Paul Duer Oct 26 '17 at 12:32
  • Press F11, and put your mouse over the part of the new you want to evaluate. – Drag and Drop Oct 26 '17 at 12:33
  • Most simple thing to do here is to scope your 'new' statement put it inside a { return new }. Once you have done that, you can put a break point on the return line. – Thomas K Oct 26 '17 at 12:55

1 Answers1

1

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.

enter image description here

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

j4nw
  • 2,227
  • 11
  • 26
  • Give the original code _also_ used an object initializer (and he/she was able to breakpoint there), I am not convinced this is what the original poster needs. – mjwills Oct 26 '17 at 12:34
  • "the real question is how do you debug Linq Expressions?" - this is what the poster needs, and this is what I answered in the first part of the answer - you debug them the same as anything else. I assumed the reason for the confusion was an object initializer, and I elaborated on it. Basically he/she wasn't able to breakpoint there in the original code either, because VS doesn't allow that. – j4nw Oct 26 '17 at 12:36
  • Yeah I like what j4nw is saying and the link he provided. I do plan to simplify my object initializer. – Paul Duer Oct 26 '17 at 12:39