我有以下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
挂断似乎是&#39;%Down&#39;行未正确投射。它在SQL中是一个十进制类型,因此有意义的是它可以在C#中输入int类型。不幸的是,当我运行程序时,我收到了一个&#34; InvalidCastException&#34;信息。请注意,如果我注释掉以下C#片段:
&#34; AvgPercDown = grp.Average(r =&gt; r.Field(&#34;%Down&#34;)),&#34;和&#34; grp.AvgPercDown,&#34;
代码&#34;工作&#34;。当然,这不是解决办法。 /耸肩
有什么想法?我怎样才能解决这个问题?我已经尝试过投射'%Down'&#39; row(在C#代码中)作为int以外的东西(例如double),但它不是那样的。
答案 0 :(得分:1)
实际上,根据我的经验,Oracle decimal没有正确转换为C#Int,使用OracleDataReaders时我必须将值作为小数并将其转换为Int。一些较新版本的ODAC似乎纠正了这个问题的部分内容。
答案 1 :(得分:0)
看起来像&#34;%Down&#34;是十进制值。在这种情况下,您应该使用:
AvgPercDown = grp.Average(r => r.Field<decimal>("% Down")),