0

我有一个快速的问题。我可以在 select 语句中执行所有这些逻辑吗?

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

做一个选择感觉不对,然后循环遍历列表并修改我刚刚选择的项目。我可以在 select 语句中执行所有这些操作吗?

感谢您的任何提示。

干杯,
~ck 在圣地亚哥

4

3 回答 3

1

当然,为什么不呢?是什么阻止您在 Select 语句中使用 ForEach 中的代码更改新的 DetectorDetails?

于 2010-07-08T15:55:18.903 回答
0

我想通了。我需要在我的投影中返回声明。

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;
}));
于 2010-07-08T16:18:56.230 回答
0

这能帮助你到达你要去的地方吗?我不能确定数据类型是否全部匹配并且会按原样编译,但这是将所有逻辑放入您的.Select(). 当然可以增强它变得更好!随意编辑此答案以使其更好地工作。

 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)
                                    }
                                  }
 );
于 2010-07-08T16:08:29.773 回答