English 中文(简体)
Linq 用于数据可数据的不正确列计算
原标题:linq to datatable incorrect column calculation

尝试用 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

最佳回答

如果我在你的位置,我会看到C#的内注限度... (2147483647)

尝试长( = Int64)

20000000000000 
>
2147483647
问题回答

根据您在代码下列出的示例数字, 您似乎再次获得整数溢出 。 请尝试使字段类型 Int64 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签