2016-03-08 45 views
1

這裏是我的腳本:無法轉換的源類型System.Nullable <double>

public PlainBrgDataSummaryComplete SummaryComputationPerTransSQLite(long ProgramID) 
{ 
    PlainBrgDataSummaryComplete result = new PlainBrgDataSummaryComplete(); 
    DataSet dataSet = GetPlainBrgDataSQLite(ProgramID); 

    var abcde = dataSet.Tables["dataBridge"] 
     .AsEnumerable() 
     //.Where(a => Convert.ToDateTime(a["reportingDate"].ToString()) >= startOfWeek1 && Convert.ToDateTime(a["reportingDate"].ToString()) < endOfWeek1.AddDays(1)) 
     .GroupBy(a => 1) 
     .Select(d => 
      new PlainBrgDataSummaryTrans 
      { 
       transactionWk6 = d.Sum(a => a.Field<double?>("TranCount")) 

      } 
     ).FirstOrDefault(); 

    result.totaltransaction = new PlainBrgDataSummaryTrans 
    { 
     transactionWk6 = abcde; 
    }; 

    return result; 
} 

我得到的錯誤 - 不能源類型轉換爲System.Nullable<double>。具體在transactionWk6 = abcde部分;

+0

是什麼transactionWk6的PlainBrgDataSummaryTrans類的類型? – Umamaheswaran

+0

public double? transactionWk6 {get;組; } –

+0

你在運行時正確的錯誤? – Umamaheswaran

回答

1

abcde已經是一個PlainBrgDataSummaryTrans類型,因爲你是投影select聲明它。

嘗試使用這樣的:

result.totaltransaction = abcde; 
return result; 
+0

如果我確實有另一個變種: 變種FGHI = dataSet.Tables [ 「Databridge針對」] .AsEnumerable() //.Where(a => Convert.ToDateTime(一個[ 「reportingDate」]的ToString() )> = startOfWeek1 && Convert.ToDateTime(一個[ 「reportingDate」]。的ToString()) 1) 。選擇(d => 新PlainBrgDataSummaryTrans { transactionWk5 = d.Sum(a => a.Field (「TranCount」)) } ).FirstOrDefault(); –

+0

如何將它包含在返回結果中。我試過這個,它只返回1 result.totaltransaction = abcde; result.totaltransaction = fghi; 返回結果; –

+0

它仍然是一樣的:你將'select'語句投影到'PlainBrgDataSummaryTrans'對象中,這就是你必須在你的方法中返回的東西。 –

0

米哈伊爾Stancescu應該解決的問題。但考慮到你的返回類型,你不需要額外的任務。你可以像修改代碼如下

public PlainBrgDataSummaryComplete SummaryComputationPerTransSQLite(long ProgramID) 
{ 
DataSet dataSet = GetPlainBrgDataSQLite(ProgramID); 

return dataSet.Tables["dataBridge"] 
    .AsEnumerable() 
    //.Where(a => Convert.ToDateTime(a["reportingDate"].ToString()) >= startOfWeek1 && Convert.ToDateTime(a["reportingDate"].ToString()) < endOfWeek1.AddDays(1)) 
    .GroupBy(a => 1) 
    .Select(d => 
     new PlainBrgDataSummaryTrans 
     { 
      transactionWk6 = d.Sum(a => a.Field<double?>("TranCount")) 

     } 
    ).FirstOrDefault(); 

}

+0

返回類型是'PlainBrgDataSummaryComplete'。 – Sakura

相關問題