InvalidCastException Error caused by DB row?
本文关键字:row DB by Error caused InvalidCastException | 更新日期: 2023-09-27 18:13:15
我有以下c#代码:
private DataSet GetSummaryData(DataSet ds)
{
DataSet dsSum = new DataSet();
DataTable dtSum = new DataTable();
DataTable dataTable = ds.Tables[0];
if (dataTable != null)
{
if (dataTable.Rows.Count > 0)
{
if (dataTable.Columns.Count > 1)
{
dtSum.Columns.Add("Line Number", typeof(int));
dtSum.Columns.Add("Throughput", typeof(int));
dtSum.Columns.Add("Lost Time", typeof(int));
dtSum.Columns.Add("Pounds Made", typeof(int));
dtSum.Columns.Add("Pounds Lost", typeof(int));
dtSum.Columns.Add("Yearly Potential", typeof(int));
//Getting the Subtotal of PoundsMade based on the Line Number column
//C# linq query
var query = from row in dataTable.AsEnumerable()
group row by row.Field<int>("Linenumber") into grp
orderby grp.Key
select new
{
Linenumber = grp.Key,
TotalPoundsMade = grp.Sum(r => r.Field<int>("Pounds Made")),
AvgThroughput = grp.Average(r => r.Field<int>("Throughput")),
TotalLostTime = grp.Sum(r => r.Field<int>("Lost Time")),
AvgPercDown = grp.Average(r => r.Field<int>("% Down")),
TotalPoundsLost = grp.Sum(r => r.Field<int>("Pounds Lost")),
TotalYearlyPotential = grp.Sum(r => r.Field<int>("Yearly Potential")),
};
foreach (var grp in query)
{
dtSum.Rows.Add(grp.Linenumber, grp.TotalPoundsMade,grp.AvgThroughput,grp.TotalLostTime,
grp.AvgPercDown, grp.TotalPoundsLost, grp.TotalYearlyPotential);
string strXML = null;
strXML = strXML + "<set name='" + grp.Linenumber + "' value='" + grp.TotalPoundsMade + "'/>";
}
}
}
}
dsSum.Tables.Add(dtSum);
return dsSum;
}
如你所见,这段代码使用Linq来访问我的数据库。SQL是:
SELECT
PDT.LineNumber,
SUM(prdt.PoundsMade) as 'Pounds Made',
CAST(ROUND(SUM(CAST(prdt.PoundsMade as DECIMAL))/ (MIN(LSA.AvailableHRS) - SUM(PDT.DownTimeHrs)),0,0) as int)
as 'Throughput',
SUM(PDT.DownTimeHrs) as 'Lost Time',
Str(ROUND(CAST(SUM(PDT.DownTimeHrs) as DECIMAL)/CAST(MIN(LSA.AvailableHRS) as DECIMAL) * 100,0), 3,0) + '%'
as '% Down',
CAST((ROUND(SUM(CAST(prdt.PoundsMade as DECIMAL))/ (MIN(LSA.AvailableHRS) - SUM(PDT.DownTimeHrs)),0,0)) *
(SUM(PDT.DownTimeHrs)) as int) as 'Pounds Lost',
CAST(ROUND(SUM(CAST(prdt.PoundsMade as DECIMAL))/ (MIN(LSA.AvailableHRS) - SUM(PDT.DownTimeHrs)),0,0) as int) *
24 * 365 as 'Yearly Potential'
FROM
rpt_Line_Shift_ProdDownTime AS PDT
LEFT OUTER JOIN rpt_Line_Shift_Prod AS Prdt
ON PDT.LineNumber = Prdt.LineNumber
and PDT.ShiftNumber = Prdt.ShiftNumber
and PDT.WorkDate = Prdt.WorkDate
INNER JOIN rpt_Line_Shift_AvailableHrs AS LSA
ON PDT.LineNumber = LSA.LineNumber
and PDT.ShiftNumber = LSA.ShiftNumber
WHERE
PDT.WorkDate BETWEEN @p_From_Date and @p_Through_Date
GROUP BY
PDT.LineNumber, PDT.ShiftNumber
ORDER BY
PDT.LineNumber, PDT.ShiftNumber
挂起似乎是'% Down'行没有正确铸造。它在SQL中是十进制类型,因此在c#中可以转换为int类型是有意义的。不幸的是,当我运行程序时,我收到一个"InvalidCastException"消息。注意,如果我注释掉下面的c#代码片段:
"AvgPercDown = grp。平均(r => r. field ("% Down")),"answers"grp。AvgPercDown。"
代码"有效"。当然,这不是解决办法。/耸耸肩
任何想法吗?我该如何解决这个问题?我试过将'% Down'行(在c#代码中)转换为int(如double)以外的东西,但它不喜欢那样。
实际上,根据我的经验,Oracle decimal不能正确转换为c# Int,当使用OracleDataReaders时,我必须将值转换为十进制并将其转换为Int。一些较新版本的ODAC似乎已经修正了此问题的部分内容。
看起来"% Down"是一个十进制值。在这种情况下,你应该使用:
AvgPercDown = grp.Average(r => r.Field<decimal>("% Down")),