locked
How to use Groupby for two columns RRS feed

  • Question

  • User-543160537 posted

    Below is my code...

    DataTable abc  = zyx.AsEnurable()

    .GroupBy( r=> r.Field<string>("FirstName")

    .Select( g =>g.First())

    .CopyToTable();

    Now i need to use lastname as well in Groupby clause as well. can someone please guide me how to use that..

    Thanks

    sree

    Thursday, September 10, 2015 4:03 PM

Answers

  • User37182867 posted

    Follow the link for more examples

    http://bfy.tw/1jUp

    The gist of it is this

    .GroupBy(x => new { x.Column1, x.Column2 })

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 4:13 PM
  • User-271186128 posted

    Hi sree,

    As for this issue, I suggest you could refer to the following code:

                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[4] { new DataColumn("ID"), new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("City") });
                dt.Rows.Add(1001, "AA", "aa", "CC1");
                dt.Rows.Add(1002, "AA", "aa", "CC2");
                dt.Rows.Add(1003, "BB", "bb", "CC3");
                dt.Rows.Add(1004, "BB", "bb", "CC4");
    
                List<DataTable> tables = dt.AsEnumerable().GroupBy(row => new { FirstName = row.Field<string>("FirstName"), LastName = row.Field<string>("LastName") })
                                                    .Select(g => g.CopyToDataTable()).ToList();
    
                GridView1.DataSource = tables[0];
                GridView1.DataBind();
                GridView2.DataSource = tables[1];
                GridView2.DataBind();

    The output screenshot:

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 1:55 AM

All replies

  • User37182867 posted

    Follow the link for more examples

    http://bfy.tw/1jUp

    The gist of it is this

    .GroupBy(x => new { x.Column1, x.Column2 })

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 4:13 PM
  • User-271186128 posted

    Hi sree,

    As for this issue, I suggest you could refer to the following code:

                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[4] { new DataColumn("ID"), new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("City") });
                dt.Rows.Add(1001, "AA", "aa", "CC1");
                dt.Rows.Add(1002, "AA", "aa", "CC2");
                dt.Rows.Add(1003, "BB", "bb", "CC3");
                dt.Rows.Add(1004, "BB", "bb", "CC4");
    
                List<DataTable> tables = dt.AsEnumerable().GroupBy(row => new { FirstName = row.Field<string>("FirstName"), LastName = row.Field<string>("LastName") })
                                                    .Select(g => g.CopyToDataTable()).ToList();
    
                GridView1.DataSource = tables[0];
                GridView1.DataBind();
                GridView2.DataSource = tables[1];
                GridView2.DataBind();

    The output screenshot:

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2015 1:55 AM