c# - 从源列表中获取由分组项目列表组成的列表
问题描述
所以我有一个来自数据库的源列表,我得到了这样的结果。
List<FileInfo> FileInfosList = DataBase.GetFileInfoslist(LoginInfo);
这将返回一个包含大约 50 行 FileInfo 类型的列表。列表中的每一行有 10 个字段,两个是 starttime 和 index
double starttime is starttime of media
int index is a index number from 0 to 3
我像这样对列表进行排序
List<FileInfo> sortedList = FileInfos.OrderBy(s => s.StartTime).ThenBy(c => c.Index).ToList();
所以我需要从这个排序列表中提取行组,其中开始时间在 50 行或记录中匹配。每组可能有 1 到 4 行。而且我需要留下一个具有匹配开始时间的组列表。
例如,如果我想提取与列表中第一行的开始时间匹配的一组行,我将使用
double starttime = sortedList[0].StartTime;
List<FileInfo>() group = sortedList.Where(x => x.StartTime ==
starttime).ToList();
但我需要一种方法来遍历整个排序列表并获取具有匹配开始时间的所有组,每个组可以是 1 到 4 行(其变量),索引号保持在 0 到 3 的排序顺序中。所有这些然后组需要在我可以使用的主列表中。
所以像
List<FileInfo> GroupOfFileInfo = list of rows with matching starttimes from source list
我想结束
List<GroupOfFileInfo> groupslist for the entire source list.
我希望这很清楚。
谢谢
解决方案
正如评论所说,使用 .GroupBy()。
GroupBy 使用起来可能有点混乱,所以这里有一个示例控制台应用程序:
class Program
{
//Simple 'file info' class
public class FileInfo
{
public int Index { get; set; }
public DateTime StartTime { get; set; }
public string OtherInfo { get; set; }
}
//Set up example data.
static List<FileInfo> data = new List<FileInfo>()
{
new FileInfo{ Index = 0, StartTime = new DateTime(2019,01,01), OtherInfo="Item 0" },
new FileInfo{ Index = 1, StartTime = new DateTime(2019,01,01), OtherInfo="Item 1" },
new FileInfo{ Index = 2, StartTime = new DateTime(2019,01,01), OtherInfo="Item 2" },
new FileInfo{ Index = 3, StartTime = new DateTime(2019,02,01), OtherInfo="Item 3" },
new FileInfo{ Index = 4, StartTime = new DateTime(2019,02,01), OtherInfo="Item 4" },
new FileInfo{ Index = 5, StartTime = new DateTime(2019,02,01), OtherInfo="Item 5" },
new FileInfo{ Index = 6, StartTime = new DateTime(2019,03,01), OtherInfo="Item 6" },
new FileInfo{ Index = 7, StartTime = new DateTime(2019,04,01), OtherInfo="Item 7" }
};
static void Main(string[] args)
{
//First GroupBy what you want to group by.
var groupedList = data.GroupBy(x => x.StartTime);
//This gives a list with the number of distinct group items.
foreach (var groupEntry in groupedList)
{
//groupEntry is an IGrouping, which contains 'key' as the group key
//(a DateTime in this case, StartTime in the source list)
Console.WriteLine($"Items in Group : {groupEntry.Key}");
//each item is itself an IEnumerable of the items in that group
foreach (var groupItem in groupEntry.OrderBy(x=>x.Index))
{
Console.WriteLine($" - Index:{groupItem.Index}; OtherInfo = {groupItem.OtherInfo}");
}
}
}
}
上述输出:
Items in Group : 01/01/2019 00:00:00
- Index:0; OtherInfo = Item 0
- Index:1; OtherInfo = Item 1
- Index:2; OtherInfo = Item 2
Items in Group : 01/02/2019 00:00:00
- Index:3; OtherInfo = Item 3
- Index:4; OtherInfo = Item 4
- Index:5; OtherInfo = Item 5
Items in Group : 01/03/2019 00:00:00
- Index:6; OtherInfo = Item 6
Items in Group : 01/04/2019 00:00:00
- Index:7; OtherInfo = Item 7
推荐阅读
- flutter - Flutter 名称 FutureOr 不是类型,因此不能在 as 表达式中使用
- javascript - 从 url 路径更改 HTML ID?
- pine-script - 无法将可变参数传递给安全功能
- c++ - 使用 char 访问向量
- spring-boot - 有没有办法使用 GraphQL 的 Spring Boot 实现在 GraphQL 中实现 Facebook 样式的分页
- javascript - 无法使用迭代在主组件内呈现子组件
- java - 在 netbeans 编程作业中创建 JAR 文件错误
- flutter - 使用来自 id 的对象填充冻结模型
- arduino - IRremote 库中的 MARK_EXCESS_MICROS 是什么?
- c# - 创建自定义 xtraGrid 控件