2009-06-11 8 views
0

http://img7.imageshack.us/img7/3050/downtime.png동적 LinqToSQL 피벗 테이블 쿼리

나는 (과에서) 두 개의 달력 선택기가 나는 YEAR1 구축 할 필요가 -> 4이 동적으로합니다. 나는 또한 2006 년과 2009 년 동안 가치가있는 1 개의 항목에 대한 이중 기록을 얻고 있습니다. 그들이 원한다면 그들은 100 년을 선택할 수 있습니다. 첨부 된 이미지를 확인하십시오.

public ActionResult DownTimeSummaryTabular(int Start,int End) 
    { 
     var q = from item in new iSppms.Models.iSppmsDataContext().Incidents 
       group item by new 
       { 
        item.Supplier.Id, 
        item.Supplier.Name, 
        item.SupplierPlant,item.DownTime 
       } 
        into supplier 
        select new 
        { 
         SupplierId = supplier.Key.Id, 
         SupplierName = supplier.Key.Name, 
         SupplierPlant = supplier.Key.SupplierPlant.Plant, 
         Years = from incident in supplier 
           let year = incident.IncidentDate.Year 
           where year <= End and year >= Start 
           group incident by year into incidentForYear 
           select incidentForYear.DownTime 
        }; 

     return View(); 
    } 

답변

0

내 눈! 나는 편집 할 힘이 있었으면 좋겠다.

다음은 문제에 대한 나의 해결책 :

// This should be in your controller 
var q = from item in new iSppms.DataAccess.IncidentRepository() 
     group item by new { 
      item.Supplier.Id, 
      item.Supplier.Name, 
      item.Supplier.Plant } 
     into supplier 
     select new { 
      SupplierId = supplier.Key.Id, 
      SupplierName = supplier.Key.Name, 
      SupplierPlant = supplier.Key.Plant, 
      Years = from incident in supplier 
        let year = incident.IncidentDate.Year 
        where year <= EndYear and year >= StartYear 
        group incident by year into incidentForYear 
        select incidentForYear.DownTime.ToIntOrDefault() 
     } 

<%   
foreach (var row in q) 
{ %> 
      <tr> 
       <td> 
        <%= incident.SupplierName %> 
       </td> 
       <td> 
        <%= incident.SupplierPlant %> 
       </td> 
    <% for(var y = StartYear; y < EndYear; ++y) 
    { 
     var year = row.Years[y]; %>     
       <td> 
        <%= year.Sum() %> 
       </td> 
<% } %>     
      </tr> 
<% } %> 

그리고 이것은 변환이 좋네요 만들 수있는 확장 방법이다.

public static int ToIntOrDefault(this string value) 
{ 
    int result; 
    Int32.TryParse(value, out result); 
    return result; 
} 

보기에는 코드가 너무 많습니다. 표시되는 내용을 선택하는 작업은 int로 변환하는 것을 포함하여 컨트롤러에서 수행해야합니다. 보기는 맹목적으로 컬렉션을 반복해야합니다 ("Sum()"이 컨트롤러에서도 수행되어야한다고 주장 할 수 있음).

+0

hmmm 코드에 오류가 있습니까? –

+0

연도 <끝과 연도> = 시작 –

+0

선택 incident.DownTime.ToIntOrDefault() –