2013-12-09 2 views
2

MVC 4 응용 프로그램을 만들고 있습니다. 모델에 데이터를 채워야하는 시점이 있습니다.linq에서 다음 문제를 해결하는 방법은 무엇입니까?

내 모델은 다음과 같다

public class NonComplianceData 
{ 
    public NonComplianceData() 
    { 
     FullYearData = new List<MonthData> 
     { 
      new MonthData { Month = "January" }, 
      new MonthData { Month = "February" }, 
      new MonthData { Month = "March" }, 
      new MonthData { Month = "April" }, 
      new MonthData { Month = "May" }, 
      new MonthData { Month = "June" }, 
      new MonthData { Month = "July" }, 
      new MonthData { Month = "August" }, 
      new MonthData { Month = "September" }, 
      new MonthData { Month = "October" }, 
      new MonthData { Month = "November" }, 
      new MonthData { Month = "December" }, 
     }; 

    } 

    public int InspectorId { get; set; } 
    public string InspectorName { get; set; } 
    public IEnumerable<MonthData> FullYearData { get; set; } 
} 

public class MonthData 
{ 
    public string Month { get; set; } 
    public int TotalAuditsCompleted { get; set; } 
    public int TotalNoDefects { get; set; } 
    public float NonComplianceRate { get; set; } 
} 

나는 FullYearData 인에 문제가 있어요 내가 여기까지 갔다 데이터 ...

var InspectorData = context.COESDetails 
    .Where(x => 
     x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && 
     x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear))    
    .Select(x => x.Inspector) 
    .Where(y=>y.Id != 0) 
    .Distinct() 
    .OrderBy(x => x.Firstname) 
    .Select(ud => new NonComplianceData 
    { 
     InspectorId = ud.Id, 
     InspectorName = ud.Firstname + " " + ud.Surname, 
     FullYearData = ??????? 
    }); 

를 얻을 수 있습니다. fullYeardata를 반복하고, 매월 선택하고 더 많은 쿼리를 수행하는 방법이 필요합니다. linq을 통해이 작업을 수행 할 수 있습니까?

나는 다음을 수행하여 필요한 것을 얻을 수있었습니다. 그것의 몇 foreach 루프 ..이 linq을 통해 할 수 있습니까? InspectorName 등을 어디에 설정합니까? 이것은 당신이 무엇을 의미입니다 -

var InspectorData = 
    context.COESDetails.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear)).Select(x => x.Inspector).Where(y => y.Id != 0).Distinct().OrderBy(x => x.Firstname) 
     .Select(ud => new NonComplianceData 
      { 
       InspectorId = ud.Id, 
       InspectorName = ud.Firstname + " " + ud.Surname 
      }).ToList(); 

foreach (var Inspector in InspectorData) 
{ 
    foreach (var MonthData in Inspector.FullYearData) 
    { 
     var auditMonthYear = MonthData.Month + " " + criteria.AuditYear; 
     MonthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected); 
     MonthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any()); 
    } 
} 

어떤 도움

감사

MiddlePat을 apprecaited 것? 이

var InspectorData = context.COESDetails.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear)).Select(x => x.Inspector).Where(y => y.Id != 0).Distinct().OrderBy(x => x.Firstname) 
        .Select(ud => 
         new NonComplianceData 
         { 
          InspectorId = ud.Id, 
          InspectorName = ud.Firstname + " " + ud.Surname, 
FullYearData = Inspector.FullYearData.Select(fyd => { 
MonthData monthData = new MonthData(); 
var auditMonthYear = MonthData.Month + " " + criteria.AuditYear; 
monthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected); 
monthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any()); 

return monthData;}).ToList() 
         }).ToList(); 

답변

2
.Select(ud => new NonComplianceData 
     { 
      InspectorId = ud.Id, 
      InspectorName = ud.Firstname + " " + ud.Surname, 
      FullYearData = FullYearData.Select(fyd => fyd.Month) //results in an IEnumerable<String> 
     }); 

당신은 linqqueries에서 하위 쿼리를 만들기에 갈 수 있습니다 작동하지 않습니다. 선택한 목록에서 다른 쿼리를 적용 할 수도 있습니다. 예를 들어

:

FullYearData = Inspector.FullYearData.Select(fyd => { 
MonthData monthData = new MonthData(); 
var auditMonthYear = MonthData.Month + " " + criteria.AuditYear; 
monthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected); 
monthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any()); 

return monthData;}).ToList() 
+0

는 전체 년 현재 컨텍스트에 존재하지 않습니다 말한다. 내 질문을 업데이 트했습니다, 제발 좀 봐 ... – user2206329

+0

yeardata를 포함하는 목록은 쿼리의 범위에서 사용할 수 있습니까? – middelpat

+0

예, yeardata는 nonComplianceData – user2206329

관련 문제