Grouping is not working
-
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:00I 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 -
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:00I 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:00If 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.2020When 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.2020That'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(); -
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.2020When 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.2020That'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();