2012-08-23 41 views
0

時,我已在部分查詢以下無法識別的表達式節點:ArrayIndex使用DateTime.ParseExact

var finalResults = 

    (from o in outerJoin 
    orderby o.timeKey.timeKey.Year_Num, o.timeKey.timeKey.Month, o.Key.Key.PlantId, o.timeKey.timeKey.PhysicalUnitId 
    select new 
    { 
     IndicatorName = IndicatorName, 
     LocationName = o.timeKey.timeKey.PlantId, 
     GroupingName = o.timeKey.timeKey.PhysicalUnitId, 
     Year = o.timeKey.timeKey.Year_Num, 
     Month = o.timeKey.timeKey.Month, 
     Numerator = o.timeKey.Key.Derate_Hours != null ? o.timeKey.Key.Derate_Hours ?? 0 : 0, 
     Denominator = o.timeKey.timeKey.Hours - o.timeKey.Key.Derate_Hours ?? 0, 
     Weight = o.timeKey.timeKey.NetMaximumCapacity,       
    }).ToList(); 

的查詢工作,只是一個月的好給我一個月份和年份的格式爲:

August 2012 

我需要它的格式如下:

08 

我改變了我的代碼如下反映:

var finalResults = 

    (from o in outerJoin 
    orderby o.timeKey.timeKey.Year_Num, o.timeKey.timeKey.Month, o.Key.Key.PlantId, o.timeKey.timeKey.PhysicalUnitId 
    select new 
    { 
     IndicatorName = IndicatorName, 
     LocationName = o.timeKey.timeKey.PlantId, 
     GroupingName = o.timeKey.timeKey.PhysicalUnitId, 
     Year = o.timeKey.timeKey.Year_Num, 
     Month = DateTime.ParseExact(o.timeKey.timeKey.Month.Split(' ')[0], "MMMM", CultureInfo.CurrentCulture).Month, 
     Numerator = o.timeKey.Key.Derate_Hours != null ? o.timeKey.Key.Derate_Hours ?? 0 : 0, 
     Denominator = o.timeKey.timeKey.Hours - o.timeKey.Key.Derate_Hours ?? 0, 
     Weight = o.timeKey.timeKey.NetMaximumCapacity,       
    }).ToList(); 

但現在我收到以下錯誤:

Unrecognized expression node: ArrayIndex 

有沒有辦法做什麼,我試圖做的?更改數據庫中的格式不是一個選項。

這裏是整個查詢:

protected IList<DataResults> QueryData(HarvestTargetTimeRangeUTC ranges) 
{ 
    using (var context = new DataClassesDataContext(_connectionString)) 
    { 
     context.CommandTimeout = 240;     
     const string IndicatorName = "{DFD88372-FB87-49AC-8576-68DCBE7B00E8}"; 

     List<string> typeCodes = new List<string>() { "D1", "D2", "D3", "DP", "PD", "DM", "D4" }; 
     DataResults endItem = new DataResults(); 
     List<DataResults> ListOfResults = new List<DataResults>(); 

     var results = 

      (from v in context.vDimUnits 
      join vf in context.vFactEnergyAllocations on v.UnitKey equals vf.UnitKey 
      join vd in context.vDimGadsEvents on vf.GadsEventKey equals vd.GadsEventKey 
      join vt in context.vDimTimes on vf.TimeKey equals vt.TimeKey 
      where typeCodes.Contains(vd.GadsEventTypeCode) 
       && vt.Year_Num >= ranges.StartTimeUTC.Year 
       && vt.Year_Num <= ranges.EndTimeUTC.Year       
       && v.PhysicalUnitId != "N/A" 
       && v.PhysicalUnitId != "UNK" 
       && v.PlantId != "UNK" 
       && v.NercUnitType != "WT" 
      group vf by new { v.PlantId, v.PhysicalUnitId, v.NetDependableCapacity, vt.Year_Num, vt.Month } into groupItem 
      select new 
      { 
       groupItem.Key.Year_Num, 
       groupItem.Key.Month, 
       groupItem.Key.PhysicalUnitId, 
       groupItem.Key.NetDependableCapacity, 
       Derate_Hours = groupItem.Sum(x => (float?)x.AllocatedEnergyMwh/groupItem.Key.NetDependableCapacity), 
       groupItem.Key.PlantId, 
       Unit = groupItem.Count() 
      }); 

     var resultHours = 

      (from f in 
      (from vt in context.vDimTimes 
      from v in context.vDimUnits 
      where vt.Year_Num >= ranges.StartTimeUTC.Year 
       && vt.Year_Num <= ranges.EndTimeUTC.Year 
       && v.PhysicalUnitId != "N/A" 
       && v.PhysicalUnitId != "UNK" 
       && v.PlantId != "UNK" 
       && v.NercUnitType != "WT" 
      select new { v.PlantId, v.PhysicalUnitId, vt.Year_Num, vt.Month, vt.TimeKey, v.NetMaximumCapacity }).Distinct() 
      group f by new { f.PhysicalUnitId, f.Year_Num, f.Month, f.PlantId } into groupItem 
      select new 
      { 
       groupItem.Key.PhysicalUnitId, 
       groupItem.Key.Year_Num, 
       groupItem.Key.Month, 
       groupItem.Key.PlantId, 
       groupItem.First().NetMaximumCapacity, 
       Hours = groupItem.Count() 
      }); 

     var serviceHrsResults = 

      (from v in context.vDimUnits 
      join vf in context.vFactEnergyAllocations on v.UnitKey equals vf.UnitKey 
      join vt in context.vDimTimes on vf.TimeKey equals vt.TimeKey 
      join vus in context.vDimUnitStates on vf.UnitStateKey equals vus.UnitStateKey 

      where vus.UnitStateType != "Active" 
       && vt.Year_Num >= ranges.StartTimeUTC.Year 
       && vt.Year_Num <= ranges.EndTimeUTC.Year 
       && v.NetDependableCapacity != 0 
       && v.PhysicalUnitId != "N/A" 
       && v.PhysicalUnitId != "UNK" 
       && v.PlantId != "UNK" 
       && v.NercUnitType != "WT" 

      group vf by new { v.PlantId, vt.Year_Num, vt.Month, v.PhysicalUnitId, v.NetDependableCapacity } into groupItem 

      select new 
      { 
       groupItem.Key.Year_Num, 
       groupItem.Key.Month, 
       groupItem.Key.PhysicalUnitId, 
       groupItem.Key.NetDependableCapacity, 
       groupItem.Key.PlantId, 
       Unit = groupItem.Count() 
      }); 

     var outerJoin1 = 

      (from h in resultHours 
      join u in results on new { h.PhysicalUnitId, h.Year_Num, h.Month } equals new { u.PhysicalUnitId, u.Year_Num, u.Month } into outer 
      from grouping in outer.DefaultIfEmpty() 
      select new { timeKey = h, Key = grouping }); 

     var outerJoin2 = 

      (from h in resultHours 
      join s in serviceHrsResults on new { h.PhysicalUnitId, h.Year_Num, h.Month } equals new { s.PhysicalUnitId, s.Year_Num, s.Month } into outer2 
      from grouping in outer2.DefaultIfEmpty() 
      select new { timeKey = h, Key = grouping }); 

     var outerJoin = 

      (from a in outerJoin1 
      join b in outerJoin2 on new { a.timeKey.PhysicalUnitId, a.timeKey.Year_Num, a.timeKey.Month } equals new 
      { 
       b.timeKey.PhysicalUnitId, 
       b.timeKey.Year_Num, 
       b.timeKey.Month 
      } into outer 
      from grouping in outer.DefaultIfEmpty() 
      select new { timeKey = a, Key = grouping }).Distinct(); 


     var finalResults = 

      (from o in outerJoin 
      orderby o.timeKey.timeKey.Year_Num, o.timeKey.timeKey.Month, o.Key.Key.PlantId, o.timeKey.timeKey.PhysicalUnitId 
      select new 
      { 
       IndicatorName = IndicatorName, 
       LocationName = o.timeKey.timeKey.PlantId, 
       GroupingName = o.timeKey.timeKey.PhysicalUnitId, 
       Year = o.timeKey.timeKey.Year_Num, 
       Month = DateTime.ParseExact(o.timeKey.timeKey.Month.Split(' ')[0], "MMMM", CultureInfo.CurrentCulture).Month, 
       Numerator = o.timeKey.Key.Derate_Hours != null ? o.timeKey.Key.Derate_Hours ?? 0 : 0, 
       Denominator = o.timeKey.timeKey.Hours - o.timeKey.Key.Derate_Hours ?? 0, 
       Weight = o.timeKey.timeKey.NetMaximumCapacity,       
       }).ToList(); 

     for (int counter = 0; counter < finalResults.Count; counter++) 
     { 
      var item = finalResults[counter]; 
      endItem = new DataResults(); 
      ListOfResults.Add(endItem); 
      endItem.IndicatorName = IndicatorName; 
      endItem.LocationName = item.LocationName; 
      endItem.GroupingName = item.GroupingName; 
      endItem.Year = item.Year; 
      endItem.Month = item.Month.ToString(); 
      endItem.Numerator = item.Numerator; 
      endItem.Denominator = item.Denominator; 
      endItem.Weight = item.Weight.Value;      
     } 
     return ListOfResults; 
    } 
} 
+0

在這種情況下type是否具有timekey屬性? –

+0

什麼是'o.timeKey.timeKey'?我們不知道您要處理的是什麼數據。 (另外,如果您可以評估一次'o.timeKey.timeKey',而不是* 18次*,那麼它確實有助於可讀性。) –

+0

@JonSkeet我將發佈整個查詢。我沒有,因爲它只是討厭。 –

回答

1

我們仍然沒有真正有多大的範圍內,但如果你正在試圖做的,我會在反正一個合適的答案:)

猜它被翻譯成SQL查詢表達式中的大量的工作,你會發現它的更好的查詢分爲兩個部分:

  • 在SQL完成的,儘可能多的過濾儘可能的一部分,只有指定日e數據,但是採用「原始」格式
  • 在LINQ to Objects中完成的一部分,它可以使用您需要的任何.NET方法。

您使用AsEnumerable從使用方法Queryable有效地翻轉到那些在Enumerable。所以,你可能有:

var sqlQuery = from ... 
       orderby ... 
       select ...; 

var finalQuery = sqlQuery.AsEnumerable().Select(entry => new { 
        // Call whatever methods you like in here 
       }); 

這樣就不必問了LINQ提供程序生成SQL效仿這樣的事情:

DateTime.ParseExact(o.timeKey.timeKey.Month.Split(' ')[0], "MMMM", 
        CultureInfo.CurrentCulture).Month, 

這也意味着,你可以寫helper方法這很容易測試外查詢:

var finalQuery = sqlQuery.AsEnumerable().Select(entry => new { 
        Month = ConvertYearMonthToMonthNumber(entry.Month), 
        ... 
       }); 

注意CultureInfo.CurrentCulture很可能是錯誤的文化這裏,除非你真的知道存儲在數據庫中的數據與用戶的文化相同,這可能不是不變的文化。我認爲你更希望使用不變文化......或僅僅使用Calendar.MonthNames.IndexOf(month)

+0

謝謝你的幫助 –

相關問題