The original SQL Command coded by the old programmers looks like this
SELECT ooe_general_id, SUM(CAST(CAST(amount AS money) AS float)) AS totalAppro, MAX(date) AS lastDate
FROM dbo.pmTA_OoeGeneralAppropriation
Datatype: String
Database Values
----------------
3,200,000.00
2916410
28,710,000.00
0.80000000000291
-1000000
When I try to convert the above code to Linq, an error 'Input string was not in a correct format' shows in this line
totalAppro = g.Sum(p => Convert.ToDouble(p.amount))
I tried doing changing the code to
Convert.Double(string, IFormatprovider)
Convert.ToDouble(String.Format("{0:c}", p.amount))
Convert.ToDecimal(p.amount.Replace(",",""))
from these Currency, Convert, SUM LINQ forums but the error still persists.
The question is what am I doing wrong here? Why did my first two codes work but this doesn't? And lastly, why is there an 'Input string was not in a correct format' error?
Thanks in advance.
UPDATE
var totalAppropriationGeneralFund = (from p in db.iBudget_OoeGeneralAppropriation
where !p.amount.StartsWith("-")
group p by p.ooe_general_id into g
select g).AsEnumerable().Select(g => new {
id = g.Key,
totalAppro = g.Sum(p => Convert.ToDecimal(p.amount.Replace(",",""))),
date = g.Max(p => p.date)
}).ToList();
amountis not in a valid format to be parsed byConvert.ToDouble(). The examples you have given thus far are fine. Check the rest of your data. It does concern me thatamountis being cast tomoneyin the original query. Why is it not being stored asmoney?