score:0
Update as per Krish comment:
Arrange all sorted groups in ascending order by their first item
var results = StudentsList
.OrderBy(x => x.Student)
.GroupBy(x => x.GroupID)
.SelectMany(x => x.OrderBy(y => y.Student))
.ToList();
Update as per YoKidYo comment
var results = StudentsList
.GroupBy(x => x.GroupID)
.OrderByDescending(x => x.Key)
.SelectMany(x => x.OrderBy(y => y.Student))
.ToList();
foreach (var item in results)
{
Console.WriteLine(item.GroupID + " : " + item.Student);
}
result :
3 : Adam
3 : Jacob
2 : Donal
2 : Olivi
1 : Jack
1 : John
1 : Peter
Old code
You can order by descending the Keys
(groupId), and order by ascending student names inside each group, like the following code :
var results = StudentsList
.GroupBy(x => x.GroupID)
.OrderByDescending(x => x.Key)
.Select(x => new { Group = "Group " + x.Key, Students = x.Select(y => y.Student).OrderBy(z => z).ToList() })
.ToList();
I hope this will help you out.
score:1
So the Linq query has sorted the data for you but now you need to display it. I have tested and it displays your expected output. Code below:
static void Main(string[] args)
{
List<StudentInfo> studentsList = new List<StudentInfo>();
studentsList.Add(new StudentInfo { Student = "John", University = "ABC", GroupID = 1, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Jack", University = "DEF", GroupID = 1, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Peter", University = "GHI", GroupID = 1, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Olivia", University = "ABC", GroupID = 2, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Donald", University = "JKL", GroupID = 2, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Adam", University = "GHI", GroupID = 3, IsQualified = false });
studentsList.Add(new StudentInfo { Student = "Jacob", University = "ABC", GroupID = 3, IsQualified = false });
var results = studentsList.OrderBy(x => x.Student)
.GroupBy(x => x.GroupID);
foreach (var group in results)
{
Console.WriteLine(group.Key);
foreach (var student in group)
{
Console.WriteLine(student.Student);
}
Console.WriteLine(Environment.NewLine);
}
}
public class StudentInfo
{
public string Student { get; set; }
public string University { get; set; }
public int GroupID { get; set; }
public bool IsQualified { get; set; }
}
score:1
var groupedStudents = studentsList.GroupBy(a => a.GroupID, (key, Students) => new
{
Key = key,
StudentsList = Students.ToList()
}).OrderByDescending(a => a.Key).ToList();
foreach (var item in groupedStudents)
{
Console.WriteLine("Group " + item.Key + ":");
foreach (var stdList in item.StudentsList.OrderBy(a => a.Student))
{
Console.WriteLine(stdList.Student);
}
Console.WriteLine();
}
Output:
Group 3:
Adam
Jacob
Group 2:
Donald
Olivia
Group 1:
Jack
John
Peter
Source: stackoverflow.com
Related Articles
- Using Linq to Sort Students within Groups and then Rearrange Group Order
- Using Linq Grouping how to I order by specific Group then remaining Groups descending
- LINQ group by then order groups of result
- Sort list of lists using LINQ so high score is first then lastname and firstname in alphabetic order
- C# LINQ group collection by attribute then sort each group by explicit order defined by a list
- Order by descending group by Max date, then order by single row in the group by date ascending using linq Method sintax
- How to use LINQ to order within groups
- Linq to SQL using group By, and order by count
- NHIbernate (3.1) - Linq group by then order by count issue
- Group by date range , count and sort within each group LINQ
- Sort collection within collection using Linq
- Order of List using Linq is not the same as sort
- Ordering within groups using LINQ
- C# LINQ - Group List by a property then select by different groups
- Linq - Group then compare elements within each group
- Linq / Entity Group by then count those groups
- Linq - Group By Id, Order By and Then select top 5 of each grouping
- Linq OrderBy using a custom sort order
- LINQ group by date and then order by property, to get the highest amount?
- Two level group by, order by using LINQ
- How to get top 1 record of each group order by desc in SQL Server using linq
- How to group and process in the order of attribute using LINQ method syntax?
- Simple linq query using group in order to return a one to many relationship
- Linq OrderBy then GroupBy - group by unexpectedly changes order of list
- C# LINQ Group and sum List<T> then Group and sum another list within the first List
- Converting SQL group by and order by to Linq using case
- LINQ or Lamba to sort an array of integers in ascending order within the set of their frequency of repetition
- Sort in descending order using LINQ query syntax
- How can I do a group by and then an average using LINQ
- How to use LINQ to group by one field, sort within that grouping, and sum values for that combination?
- Combine data gathered from different sources into one IEnumerable object
- Linq to Querystring substringof
- Refactoring for-loop Linq query into a single query
- Summing columns using LINQ
- XML To Dictionary
- How to sort an OrderedDictionary using Linq in C# (using .NET 3.5)?
- Is there a way to inline external functions into an EF Linq query?
- How to Group and sum values of two Column in C# Linq
- NHibernate produce System.ArgumentException
- Linq Unit Test Suite
- Why is this LINQ query not returning the correct dates?
- Convert IQueryable to IOrderedQueryable
- flatten result from linq query
- LINQ .ToListAsync failing when using two tables
- How to set Object instance value equal null if there is no data found in Left Join in LINQ Query
- System.Linq.Expressions.Expression for .OrderBy function
- Understanding ClientContext.Load's Parameters
- Inside of using block, need to dispose and reinstantiate
- Which is faster in .NET, .Contains() or .Count()?
- Linq: Considering table as not nothinh in a if statement even if table is empty