问题描述
我希望对通用列表进行分组和排序.我有一个代表文件的对象列表,每个对象都有一个FileName,FileType和FileDate属性. FileType定义为枚举.
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.
我有一些工作代码,可让我按FileType将文件列表分组在一起.
I have some working code which allows me to group together lists of files by FileType.
var fileGroups = fileList.GroupBy(f=> f.FileType)
foreach (var group in fileGroups )
{
foreach (var file in group)
{
}
}
我想做的是按FileType枚举值对fileGroups进行排序,然后按FileDate对fileGroups中的每个组进行排序.
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
您需要对组进行排序(1),将每个组在内部进行排序(2);
You need the groups sorted (1) and each of the groups sorted internally (2);
这篇关于C#分组/排序通用列表使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!