Home > database >  Explicit conversion when using the sum function of Linq GroupBy (C#)
Explicit conversion when using the sum function of Linq GroupBy (C#)

Time:06-14

I need help with using Linq GroupBy and Sum. This part of the program I'm coding returns error Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)"



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

I've manage to pinpoint the issue to the line

histAngsuran = x.Sum(x => x.hist_angsuran)

I've tried converting it to Int32 but the value exceeds the MaxValue. Any help is appreciated. Thank you.

CodePudding user response:

This is just a wild guess but the problem might by that histix contains dynamic objects. It could be that the compiler is unable to select the correct overload of Sum() and defaults to the implementation for int. During runtime this then crashes with the error you posted because the decimal hist_angsuran (hidden behind dynamic) does not fit the implementation.

Try helping out the compiler by doing:

histAngsuran = x.Sum(x => (decimal)x.hist_angsuran),

Update:

Yup, I just verified it. You are getting a RuntimeBinderException without the cast to (decimal):

RuntimeBinderException: Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

Here's a minimal reproducible example:

IEnumerable<dynamic> histix = Enumerable
    .Range(0, 10)
    .Select(i => new { hist_angsuran = (decimal)i });

var histixSum = histix.Sum(x => x.hist_angsuran);

CodePudding user response:

Try converting it to Int64.

Int32 has a max value of 2147483647; source

Int64 has a max value of 9223372036854775807. source

  • Related