C# Grouping/Sorting a Generic List<> using LINQ
Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an enumeration.
I have some working code which allows me to group tog开发者_开发知识库ether lists of files by FileType.
var fileGroups = fileList.GroupBy(f=> f.FileType)
foreach (var group in fileGroups )
{
foreach (var file in group)
{
}
}
What I would like to be able to do is order fileGroups by the FileType enumeration value and then each group within fileGroups by the FileDate.
var sortedThing = fileGroups
.OrderBy(g => g.Key) // (1) Order the groups
.Select(g => g.OrderBy(f => f.FileDate)); // (2) Order *each* group
You need the groups sorted (1) and each of the groups sorted internally (2);
精彩评论