views:

37

answers:

3

Hi All,
I had a quick question. Can I do all of this logic inside the select statement?

 var entries = atisDAO.GetPME(xl, null);
 response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme => new DetectorDetails {ID = pme.PlaceNum.ToString()}));
 if(response.Data.Detectors.Any())
 {
   response.Data.Detectors.ForEach(d =>{
      int id;
      if(int.TryParse(d.ID, out id))
      {
         var summaries = atisDAO.GetSummaryEntries(id);
         if (summaries.Any())
         {
             var count = summaries.Sum(summary => summary.TODCount + summary.BFICount + summary.ViolationCount);
             var detectionDate = summaries.Max(summary => summary.ReadDate);

             d.Count = count.ToString();
             d.DetectionTime = new DateTimeZone {
                  ReadDate = detectionDate.ToString(DATE_FORMAT)
                , ReadTime = detectionDate.ToString(TIME_FORMAT)
             };
           }
         }
     });
 }

It feels wrong to do a select, then loop through the list and modify the items I just selected. Can I do all of this inside the select statement?

Thanks for any tips.

Cheers,
~ck in San Diego

+1  A: 

Sure, why not? What's stopping you from changing the new DetectorDetails, with the code from the ForEach, in the Select statement?

Qwertie
I can't get the syntax correct. Visual Studio keeps barking at me.
Hcabnettek
A: 

Does this help get you to where you're going? I can't be sure that the datatypes all match and will compile as-is, but it's a stab at putting all that logic in your .Select(). Surely it can be enhanced to be better! Feel free to edit this answer to make it work better.

 response.Data.Detectors = atisDAO.GetPME(xl, null).Select(pme =>
                new DetectorDetails{
                                    ID = pme.PlaceNum.ToString(),
                                    Count = atisDAO.GetSummaryEntries(int.Parse(pme.PlaceNum.ToString())).Count(), //some work needed here to ensure pme.PlaceNum is actually an number 
                                    DetectionTime = new DateTimeZone{
                                        ReadDate = summaries.Max(summary => summary.ReadDate).ToString(DATE_FORMAT),
                                        ReadTime = summaries.Max(summary => summary.ReadDate).ToString(TIME_FORMAT)
                                    }
                                  }
 );
p.campbell
A: 

I figured this out. I needed a return statement inside my projection.

var entries = atisDAO.GetPME(xl, null);
response.Data.Detectors = new List<DetectorDetails>(entries.Select(pme =>{
    var details = new DetectorDetails { ID = pme.PlaceNum.ToString()};
    var summaries = atisDAO.GetSummaryEntries(pme.PlaceNum);
    if (summaries.Any())
    {
        var count = summaries.Sum(summary => summary.TODCount + summary.BFICount + summary.ViolationCount);
        var detectionDate = summaries.Max(summary => summary.ReadDate);

        details.Count = count.ToString();
        details.DetectionTime = new DateTimeZone {
            ReadDate = detectionDate.ToString(DATE_FORMAT)
            , ReadTime = detectionDate.ToString(TIME_FORMAT)
        };
     }

     return details;
}));
Hcabnettek