Asp.net 动态LinqToSQL透视表查询

Asp.net 动态LinqToSQL透视表查询,asp.net,asp.net-mvc,linq-to-sql,Asp.net,Asp.net Mvc,Linq To Sql,我有两个日历选择器(往返),我需要动态地构建Year1->4。我还获得了1个具有2006年和2009年值的项目的重复记录。他们也可以选择100年,如果他们愿意的话 public ActionResult DownTimeSummaryTabular(int Start,int End) { var q = from item in new iSppms.Models.iSppmsDataContext().Incidents group

我有两个日历选择器(往返),我需要动态地构建Year1->4。我还获得了1个具有2006年和2009年值的项目的重复记录。他们也可以选择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();
    }
public ActionResult downtimesummary表格(int-Start,int-End)
{
var q=来自新iSppms.Models.iSppmsDataContext()事件中的项目
按新建项目分组
{
item.Supplier.Id,
item.Supplier.Name,
项目.供应商工厂,项目.停工期
}
进入供应商
选择新的
{
SupplierId=supplier.Key.Id,
供应商名称=supplier.Key.Name,
供应商工厂=supplier.Key.SupplierPlant.Plant,
年=从供应商的事件开始
let year=事件.IncidentDate.year
其中年份=开始
将每年发生的事件分组为意外事件
选择incidentForYear.DownTime
};
返回视图();
}

我的眼睛!我希望我有编辑的能力

以下是我对这个问题的解决方案:

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

请注意,视图中的代码太多了。选择显示内容的工作应该在控制器中完成,包括转换为int。视图应该盲目地迭代集合(可以说“Sum()”也应该在控制器中完成)。

where year=Startselect incident.DownTime.ToIntorderFault()它不知道incident应该是incident ForYear,将修复。您可能需要根据您的实际代码调整此代码。您是否在msn/google talk上?加我jeanres@bluegrassdigital.com
 public static int ToIntOrDefault(this string value)
 {
  int result;
  Int32.TryParse(value, out result);
  return result;
 }