none
Group by problem RRS feed

  • Question

  • Group by is super easy in T-Sql.
    How can I do the same thing in Linq:

    var query = from c in mo.CONTRACTS
                            group c by c.piin_spiin_cont into s
                            orderby s.Key
                            select s;
                List<string> piins = query.ToList<string>();

    does not compile.

    Tuesday, December 20, 2016 6:36 PM

Answers


  • Hi Arne Garvan,

    You need to use the following format to get each set of objects.

       var q = from p in ImageModels
                              group p by p.ID into g
                              orderby g.Key
                              select g;
                foreach (var gp in q)
                {
                    Console.WriteLine("Group NO: " + gp.Key.ToString());
                    foreach (ImageModels item in gp)
                    {
                        //do something
                        Console.WriteLine(item.ID + ":" + item.ImgUrl);
                    }
                    Console.WriteLine("-----------------");
                }
                //List<ImageModels> piins = q.ToList<ImageModels>();


    The other way for your reference.

    var q = (from p in ImageModels
                         group p by p.ID into g
                         orderby g.Key
                         select new { g, g.Key }).ToList();
    
                for (int i = 0; i < q.Count; i++)
                {
                    var gp = q[i];
                    Console.WriteLine("Group NO: " + gp.Key.ToString());
                    List<ImageModels> imagess = gp.g.ToList();
                    Console.WriteLine("ImageModels count:: " + imagess.Count);
                }

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arne Garvan Wednesday, December 21, 2016 1:57 PM
    Wednesday, December 21, 2016 2:38 AM
    Moderator

All replies


  • Hi Arne Garvan,

    You need to use the following format to get each set of objects.

       var q = from p in ImageModels
                              group p by p.ID into g
                              orderby g.Key
                              select g;
                foreach (var gp in q)
                {
                    Console.WriteLine("Group NO: " + gp.Key.ToString());
                    foreach (ImageModels item in gp)
                    {
                        //do something
                        Console.WriteLine(item.ID + ":" + item.ImgUrl);
                    }
                    Console.WriteLine("-----------------");
                }
                //List<ImageModels> piins = q.ToList<ImageModels>();


    The other way for your reference.

    var q = (from p in ImageModels
                         group p by p.ID into g
                         orderby g.Key
                         select new { g, g.Key }).ToList();
    
                for (int i = 0; i < q.Count; i++)
                {
                    var gp = q[i];
                    Console.WriteLine("Group NO: " + gp.Key.ToString());
                    List<ImageModels> imagess = gp.g.ToList();
                    Console.WriteLine("ImageModels count:: " + imagess.Count);
                }

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arne Garvan Wednesday, December 21, 2016 1:57 PM
    Wednesday, December 21, 2016 2:38 AM
    Moderator
  • That works, but performance is not good.
    Each of your examples took 1 second to run for me. The same thing in T-SQL takes 150ms.

    • Edited by Arne Garvan Wednesday, December 21, 2016 2:33 PM
    Wednesday, December 21, 2016 1:57 PM