2010-07-08 1 views
0

나는 빠른 질문을했다. select 문에서이 모든 논리를 수행 할 수 있습니까?Linq IEnumerable 선택 질문 - 내 선택 내에서이 모든 작업을 수행 할 수 있습니까?

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

답변

1

물론, 왜? Select 문에서 ForEach의 코드를 사용하여 새로운 DetectorDetails를 변경하지 못하게하는 요인은 무엇입니까?

+0

올바른 구문을 가져올 수 없습니다. Visual Studio는 계속 짖고 있습니다. – Hcabnettek

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

나는 이것을 알아 냈다. 나는 내 계획안에 return statement가 필요했다.

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; 
})); 
관련 문제