尝试用 Linq 计算列值。 它总和正确, 但在除法或乘法时产生错误的值。 示例如下 。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dtWidgets = Get_widgets();
DataTable dtHits = Get_hits();
var hit_rate =
from w in dtWidgets.AsEnumerable()
join h in dtHits.AsEnumerable()
on w[0] equals h[0]
select new
{
Date = w.Field<DateTime>("calendar_date"),
Widgets = w.Field<int>("widgets"),
Hits = h.Field<int>("hits"),
TestSum = w.Field<int>("widgets")+h.Field<int>("hits"),
TestMult = w.Field<int>("widgets") * h.Field<int>("hits")
};
gvWidgets.DataSource = dtWidgets;
gvWidgets.DataBind();
gvHits.DataSource = dtHits;
gvHits.DataBind();
gvLinq.DataSource = hit_rate.ToArray();
gvLinq.DataBind();
}
static DataTable Get_widgets()
{
DataTable widgets = new DataTable();
widgets.Columns.Add("calendar_date", typeof(DateTime));
widgets.Columns.Add("widgets", typeof(int));
widgets.Rows.Add("05/15/2012", 200000);
widgets.Rows.Add("05/16/2012", 210000);
return widgets;
}
static DataTable Get_hits()
{
DataTable hits = new DataTable();
hits.Columns.Add("calendar_date", typeof(DateTime));
hits.Columns.Add("hits", typeof(int));
hits.Rows.Add("05/15/2012", 100000000);
hits.Rows.Add("05/16/2012", 120000000);
return hits;
}
}
返回时间 :
日期 部件点击测试Sum 测试模块
5/15/2012 中午12:00 200000 100000 100200000-1662697472
5/16/2012 中午12:00 2100 120000 000 120210000 1426874368