开发者

Linq IEnumerable Select Question - Can I do all of this inside my select?

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


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


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)
                                    }
                                  }
 );


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;
}));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜