gpt4 book ai didi

c# - 将分层字典转换为 List>()

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:48 25 4
gpt4 key购买 nike

我有一个类似于 Dictionary<Document, File[ n ]> 的字典我想将该词典转换为 List<KeyValuePair<Document, File>>这是扁平系统

目前我正在使用 foreach 转换该列表,是否可以使用 Linq?

     var documentFileList = RepositoryFactory.FileRepository.GetFiles(_case);

var retVal = new List<KeyValuePair<Document, File>>();
if (documentFileList != null)
{
foreach (var docFileKeyPair in documentFileList)
{
if (docFileKeyPair.Value != null)
{
foreach (var fileEntity in docFileKeyPair.Value)
{
var document = docFileKeyPair.Key as Document;
if (document != null)
{
retVal.Add(new KeyValuePair<Document, File>(document, fileEntity));
}
}
}
}
}

提前致谢

最佳答案

使用 Linq,您可以做到这一点。

var flattenList = documentFileList.SelectMany(x=>x.Value.Where(v=> v != null).Select(s=> new KeyValuePair<Document, File>(x.Key, s)))
.ToList();

检查这个example code

关于c# - 将分层字典转换为 List<KeyValuePair<Document,File>>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com