locked
Groupby multipe columns RRS feed

  • Question

  • User-543160537 posted

    Hi,

    I want to group by multple columns in LINQ, how to achieve that below is the code i tried..below i am getting error..

     DataTable uniquemgmtcmdetails = meetingdetails.AsEnumerable()
                                   .GroupBy(r => r.Field<string>("Title"),r => r.Field<string>("Name"))
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    Thanks

    Sree
     

    Friday, September 25, 2015 12:24 PM

Answers

  • User-271186128 posted

    Hi Sree,

    how to achieve that below is the code i tried..below i am getting error..

     DataTable uniquemgmtcmdetails = meetingdetails.AsEnumerable()
                                   .GroupBy(r => r.Field<string>("Title"),r => r.Field<string>("Name"))
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    As for this issue, please modify your code as below:

     DataTable uniquemgmtcmdetails = meetingdetails.AsEnumerable()
                                   .GroupBy(r => new { Title = r.Field<string>("Title"), Name = r.Field<string>("Name")})
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2015 11:28 PM

All replies

  • User603616845 posted

    Hi,

    If you want to group by with multiple column than you need to pass it in Group By clause like this 

    group x by new { x.Column1, x.Column2 }

    Please find a demo example to understand the group by.

    var myvalue=
        from c in children
        group c by new
        {
            c.School,
            c.Friend,
            c.FavoriteColor,
        } into gcs
        select new ConsolidatedChild()
        {
            School = gcs.Key.School,
            Friend = gcs.Key.Friend,
            FavoriteColor = gcs.Key.FavoriteColor,
            Children = gcs.ToList(),
        };

    Hope this will help you.

    thanks

    Friday, September 25, 2015 1:19 PM
  • User-271186128 posted

    Hi Sree,

    how to achieve that below is the code i tried..below i am getting error..

     DataTable uniquemgmtcmdetails = meetingdetails.AsEnumerable()
                                   .GroupBy(r => r.Field<string>("Title"),r => r.Field<string>("Name"))
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    As for this issue, please modify your code as below:

     DataTable uniquemgmtcmdetails = meetingdetails.AsEnumerable()
                                   .GroupBy(r => new { Title = r.Field<string>("Title"), Name = r.Field<string>("Name")})
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2015 11:28 PM