locked
Order By Row Total in Datatable using Linq RRS feed

  • Question

  • User-1971168174 posted

    Hi,

    I am trying to do order by sum of row using linq

    datatable has following data:

    category	A10	A20
    A	         1	2
    B	         5	3
    C	         2	1
    D	         8	1
    E	         7	5
    

    output should be :

    Category	A10	A20
    E	         7	5
    D	         8	1
    B	         5	3
    A	         1	2
    C	         2	1
    

    E=12,D=9,B=8,A=3,C=3

    Please suggest.

    Thanks!

    Tuesday, March 8, 2016 1:34 AM

Answers

  • User-271186128 posted

    Hi neerajkumarmodi,

    I suggest you could refer to the following code:

                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("category", typeof(string)), new DataColumn("A10", typeof(Int32)), new DataColumn("A20", typeof(Int32)) });
                dt.Rows.Add('A', 1, 2);
                dt.Rows.Add('B', 5, 3);
                dt.Rows.Add('C', 2, 1);
                dt.Rows.Add('D', 8, 1);
                dt.Rows.Add('E', 7, 5);
    
                var query = from tt in dt.AsEnumerable()
                            orderby Convert.ToInt32(tt.Field<int>("A10").ToString()) + Convert.ToInt32(tt.Field<int>("A20").ToString()) descending
                            select new {
                                category = tt.Field<string>("category").ToString(),
                                A10 = tt.Field<Int32>("A10").ToString(),
                                A20 = tt.Field<Int32>("A20").ToString()
                            };
                GridView2.DataSource = query.ToList();
                GridView2.DataBind();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 6:19 AM

All replies

  • User-271186128 posted

    Hi neerajkumarmodi,

    I suggest you could refer to the following code:

                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("category", typeof(string)), new DataColumn("A10", typeof(Int32)), new DataColumn("A20", typeof(Int32)) });
                dt.Rows.Add('A', 1, 2);
                dt.Rows.Add('B', 5, 3);
                dt.Rows.Add('C', 2, 1);
                dt.Rows.Add('D', 8, 1);
                dt.Rows.Add('E', 7, 5);
    
                var query = from tt in dt.AsEnumerable()
                            orderby Convert.ToInt32(tt.Field<int>("A10").ToString()) + Convert.ToInt32(tt.Field<int>("A20").ToString()) descending
                            select new {
                                category = tt.Field<string>("category").ToString(),
                                A10 = tt.Field<Int32>("A10").ToString(),
                                A20 = tt.Field<Int32>("A20").ToString()
                            };
                GridView2.DataSource = query.ToList();
                GridView2.DataBind();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 6:19 AM
  • User-1971168174 posted

    Thanks Zhi !

    Its working.

    Tuesday, March 8, 2016 5:55 PM