Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Grouping is not working

Grouping is not working

Scheduled Pinned Locked Moved LINQ
csharplinqhelp
3 Posts 2 Posters 30 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lambigo2
    wrote on last edited by
    #1

    Dear all, I'm new in Linq. I have code like the one listed below. Grouping is not working (see output results below) and shows all records. I would like that it would group by ID and order by birthday (Desc), so I would get results like this:

    ID: 1 Name: Arnold; BirthDay: 15.06.2020 00:00:00
    ID: 2 Name: Stefan; BirthDay: 15.06.2020 00:00:00

    I would appreciate for any help.

    class Program
    {
    public class Student
    {
    public int ID { get; set; }
    public int StandardID { get; set; }
    public string Name { get; set; }
    public DateTime Birthdate { get; set; }
    }

        static void Main(string\[\] args)
        {
            List studentList = new List() {
                new Student() { ID = 1, Name = "Arnold", Birthdate = new DateTime(2020, 06, 15)},
                new Student() { ID = 1, Name = "Arnold", Birthdate = new DateTime(2020, 01, 01)},
                new Student() { ID = 2, Name = "Stefan", Birthdate = new DateTime(2020, 06, 15)},
                new Student() { ID = 2, Name = "Stefan", Birthdate = new DateTime(2020, 06, 01)},
            };
            
            int\[\] Ids = { 1, 2, 3 };
    
            List students = studentList
                .Where(s => Ids.Contains(s.ID) )
                .GroupBy(s => s.ID)
                .SelectMany(g => g.OrderByDescending(s => s.Birthdate) ).ToList();
    
            foreach (var i in students)
            {   
                Console.WriteLine($"ID: {i.ID} Name: {i.Name}; BirthDay: {i.Birthdate} ");
            }
            Console.ReadKey();
        }
    

    Results of above:

    ID: 1 Name: Arnold; BirthDay: 15.06.2020 00:00:00
    ID: 1 Name: Arnold; BirthDay: 01.01.2020 00:00:00
    ID: 2 Name: Stefan; BirthDay: 15.06.2020 00:00:00
    ID: 2 Name: Stefan; BirthDay: 01.06.2020 00:00:00

    F 1 Reply Last reply
    0
    • L lambigo2

      Dear all, I'm new in Linq. I have code like the one listed below. Grouping is not working (see output results below) and shows all records. I would like that it would group by ID and order by birthday (Desc), so I would get results like this:

      ID: 1 Name: Arnold; BirthDay: 15.06.2020 00:00:00
      ID: 2 Name: Stefan; BirthDay: 15.06.2020 00:00:00

      I would appreciate for any help.

      class Program
      {
      public class Student
      {
      public int ID { get; set; }
      public int StandardID { get; set; }
      public string Name { get; set; }
      public DateTime Birthdate { get; set; }
      }

          static void Main(string\[\] args)
          {
              List studentList = new List() {
                  new Student() { ID = 1, Name = "Arnold", Birthdate = new DateTime(2020, 06, 15)},
                  new Student() { ID = 1, Name = "Arnold", Birthdate = new DateTime(2020, 01, 01)},
                  new Student() { ID = 2, Name = "Stefan", Birthdate = new DateTime(2020, 06, 15)},
                  new Student() { ID = 2, Name = "Stefan", Birthdate = new DateTime(2020, 06, 01)},
              };
              
              int\[\] Ids = { 1, 2, 3 };
      
              List students = studentList
                  .Where(s => Ids.Contains(s.ID) )
                  .GroupBy(s => s.ID)
                  .SelectMany(g => g.OrderByDescending(s => s.Birthdate) ).ToList();
      
              foreach (var i in students)
              {   
                  Console.WriteLine($"ID: {i.ID} Name: {i.Name}; BirthDay: {i.Birthdate} ");
              }
              Console.ReadKey();
          }
      

      Results of above:

      ID: 1 Name: Arnold; BirthDay: 15.06.2020 00:00:00
      ID: 1 Name: Arnold; BirthDay: 01.01.2020 00:00:00
      ID: 2 Name: Stefan; BirthDay: 15.06.2020 00:00:00
      ID: 2 Name: Stefan; BirthDay: 01.06.2020 00:00:00

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      If you think about the data in your grouping;

      1
      Arnold 15.06.2020
      Arnold 01.01.2020
      2
      Stefan 15.06.2020
      Stefan 01.06.2020

      When you do SelectMany on that group you are flattening the tree structure one level as you're selecting the children of each parent item so your SelectMany looks like

      Arnold 15.06.2020
      Arnold 01.01.2020
      Stefan 15.06.2020
      Stefan 01.06.2020

      That's why you're seeing all of the results. What you actually want to do is select the first item in each group after ordering it so change to .Select and do a .First on the ordered group.

      List students = studentList
      .Where(s => Ids.Contains(s.ID))
      .GroupBy(s => s.ID)
      .Select(g => g.OrderByDescending(s => s.Birthdate).First())
      .ToList();

      L 1 Reply Last reply
      0
      • F F ES Sitecore

        If you think about the data in your grouping;

        1
        Arnold 15.06.2020
        Arnold 01.01.2020
        2
        Stefan 15.06.2020
        Stefan 01.06.2020

        When you do SelectMany on that group you are flattening the tree structure one level as you're selecting the children of each parent item so your SelectMany looks like

        Arnold 15.06.2020
        Arnold 01.01.2020
        Stefan 15.06.2020
        Stefan 01.06.2020

        That's why you're seeing all of the results. What you actually want to do is select the first item in each group after ordering it so change to .Select and do a .First on the ordered group.

        List students = studentList
        .Where(s => Ids.Contains(s.ID))
        .GroupBy(s => s.ID)
        .Select(g => g.OrderByDescending(s => s.Birthdate).First())
        .ToList();

        L Offline
        L Offline
        lambigo2
        wrote on last edited by
        #3

        Thank you very much for quick answer!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups