none
Linq Query to sum DataRows RRS feed

  • Question

  • I need to sum corresponding elements in  two data rows except the first element. The first element  is string.  how can i do this?

    Here the corresponding rows have same data types. But each row contain Int32, decimal data type values. 

    The resulting sum should be inserted into the datatable as the last row. The first element in the row is string 'Total'. I need to sum of the remaining values.

    row 1: Total A, 200, 222.567, 300, 400.555  

    row 2: Total B, 300, 111.432,600, 300.444

    Result: Grand Total, 500,  333.999, 900, 700.999


    • Edited by wpf_xam1 Friday, October 14, 2016 1:25 AM
    Friday, October 14, 2016 1:07 AM

Answers

  • Hi wpf_xam1,

    >>Here  there may be variable number of items in the data row and the datatype of each element  is not known. The datatype of any element can be any of these data types: Int32, Int64, Decimal. How can we sum in this case?

    It seems that we need to use lamda Expression to implement it.

    http://stackoverflow.com/questions/16516971/linq-dynamic-select

    In addition, it seems there is a simple way by using datatable.compute menhod to achieve it. like this:

     var result = tblDatas.Compute("sum(c1)","");

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 17, 2016 8:19 AM
    Moderator

All replies

  • Hi wpf_xam1,

    According to your description, I create a demo as below for your reference.

    DataTable tblDatas = new DataTable("Datas");
                tblDatas.Columns.Add("Total", Type.GetType("System.String"));
                tblDatas.Columns.Add("C1", Type.GetType("System.Int32"));
                tblDatas.Columns.Add("C2", Type.GetType("System.Decimal"));
                tblDatas.Columns.Add("C3", Type.GetType("System.Int32"));
                tblDatas.Columns.Add("C4", Type.GetType("System.Decimal"));
                tblDatas.Rows.Add(new object[] { "Total A", 200, 222.567, 300, 400.555 });
                tblDatas.Rows.Add(new object[] { "Total B", 300, 111.432, 600, 300.444 });
    
    
                var rrr = from ord in tblDatas.AsEnumerable()
                            group ord by 1 into t
                            select new
                            {
                                Total = "Total",
                                C1 = t.Sum(c => c.Field<int>("C1")),
                                C2 = t.Sum(c => c.Field<decimal>("C2")),
                                C3 = t.Sum(c => c.Field<int>("C3")),
                                C4 = t.Sum(c => c.Field<decimal>("C4"))
                            };
                foreach (var item in rrr)
                {
                    Console.WriteLine("{0} -- {1} -- {2} -- {3} -- {4}", item.Total, item.C1, item.C2, item.C3, item.C4);
                }
                
                Console.ReadLine();

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 14, 2016 8:54 AM
    Moderator
  • The reply doesn't help me.Here  there may be variable number of items in the data row and the datatype of each element  is not known. The datatype of any element can be any of these data types: Int32, Int64, Decimal. How can we sum in this case?



    • Edited by wpf_xam1 Saturday, October 15, 2016 9:45 AM
    Friday, October 14, 2016 9:59 AM
  • Hi wpf_xam1,

    >>Here  there may be variable number of items in the data row and the datatype of each element  is not known. The datatype of any element can be any of these data types: Int32, Int64, Decimal. How can we sum in this case?

    It seems that we need to use lamda Expression to implement it.

    http://stackoverflow.com/questions/16516971/linq-dynamic-select

    In addition, it seems there is a simple way by using datatable.compute menhod to achieve it. like this:

     var result = tblDatas.Compute("sum(c1)","");

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 17, 2016 8:19 AM
    Moderator