我在使用 Linq GroupBy 和 Sum 方面需要幫助。我正在撰寫的程式的這一部分回傳錯誤無法將型別“十進制”隱式轉換為“整數”。存在顯式轉換(您是否缺少演員表?)”
private IEnumerable<dynamic> histix;
public void CreateADKTran()
{
histix = File.ReadLines(PATH_LNHISTIX)
.Skip(1)
.Where(line => line.Substring(157, 3) == "PKK" &&
Decimal.Parse(line.Substring(141, 16)) != 0 &&
line.Substring(80, 1) != "*" &&
(line.Substring(58, 1) == "6" || line.Substring(58, 3) == "310"))
.Select(line => new
{
hist_acct_no = line.Substring(0, 10).ToString(),
hist_tran_date = DateTime.ParseExact(line.Substring(61, 8), "yyyyMMdd", provider),
hist_outstanding = Decimal.Parse(line.Substring(89, 13).ToString()),
hist_angsuran = (Decimal.Parse(line.Substring(102, 2)) Decimal.Parse(line.Substring(154, 2))) >= 100 ?
(Decimal.Parse(line.Substring(141, 13) 1)) : (Decimal.Parse(line.Substring(141, 13))),
hist_txn_code = line.Substring(58, 3).ToString(),
hist_txn_type = line.Substring(58, 1).ToString()
});
var histixSum = histix.GroupBy(x => new { x.hist_acct_no, x.hist_tran_date, x.hist_txn_type })
.Select(x => new {
histAcctno = x.Key.hist_acct_no,
histTranDate = x.Key.hist_tran_date,
histTxnType = x.Key.hist_txn_type,
histOS = x.Last().hist_outstanding,
histAngsuran = x.Sum(x => x.hist_angsuran),
histTxnCode = x.Last().hist_txn_code
});
}
我已經設法確定問題所在
histAngsuran = x.Sum(x => x.hist_angsuran)
我嘗試將其轉換為 Int32,但該值超過了 MaxValue。任何幫助表示贊賞。謝謝你。
uj5u.com熱心網友回復:
這只是一個瘋狂的猜測,但問題可能在于histix包含dynamic物件。可能是編譯器無法選擇正確的多載Sum()并默認實作int. 在運行時,這會因您發布的錯誤而崩潰,因為decimal hist_angsuran(隱藏在后面dynamic)不適合實作。
嘗試通過以下方式幫助編譯器:
histAngsuran = x.Sum(x => (decimal)x.hist_angsuran),
更新:
是的,我剛剛驗證過了。你得到一個RuntimeBinderException沒有演員表(decimal):
RuntimeBinderException:無法將型別“十進制”隱式轉換為“整數”。存在顯式轉換(您是否缺少演員表?)
這是一個最小的可重現示例:
IEnumerable<dynamic> histix = Enumerable
.Range(0, 10)
.Select(i => new { hist_angsuran = (decimal)i });
var histixSum = histix.Sum(x => x.hist_angsuran);
uj5u.com熱心網友回復:
嘗試將其轉換為 Int64。
Int32 的最大值為 2147483647;資源
Int64 的最大值為 9223372036854775807。來源
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490583.html
上一篇:為什么我的XMLXElement中有一個額外的屬性?
下一篇:跳過異步/等待?
