2009-06-11 8 views
0

http://img7.imageshack.us/img7/3050/downtime.pngtableau croisé dynamique dynamique LinqToSQL requête

J'ai deux ramasseurs de calendrier (vers et à partir), je dois construire le Année1 -> 4 dynamicly. Je reçois également des enregistrements dupliqués pour 1 articles qui ont des valeurs pour 2006 et 2009. Ils peuvent choisir 100 ans s'ils le voulaient aussi. Vérifiez l'image ci-jointe.

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

Répondre

0

Mon œil! J'aimerais avoir le pouvoir de modifier.

Voici ma solution au problème:

// 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> 
<% } %> 

Et ceci est la méthode d'extension pour effectuer la conversion plus agréable.

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

Notez que vous avez trop de code dans votre vue. Le travail de choisir ce qui est affiché doit être fait dans votre contrôleur, y compris la conversion en ints. La vue devrait juste itérer aveuglément sur les collections (on pourrait argumenter que le "Sum()" devrait aussi être fait dans le contrôleur).

+0

hmmm Je reçois des erreurs sur ce code? –

+0

où l'année <= Fin et année> = Démarrer –

+0

sélectionnez incident.DownTime.ToIntOrDefault() –

Questions connexes