Dictionary 如何将字典转换为矩阵?
我有这本字典,它有以下定义:Dictionary 如何将字典转换为矩阵?,dictionary,matrix,asp.net-mvc-5,tuples,Dictionary,Matrix,Asp.net Mvc 5,Tuples,我有这本字典,它有以下定义: Dictionary<Tuple<int, int, int>, Document> 字典 字典包含如下信息: 现在我需要把它们做成这样的形式: 括号之间的数字是id。 因此,我需要在相应的ID组合下显示文档 我的文档标题应显示在“文档”列中。 在相应的语言下,我想要一个“X”。这就是我们解决问题的方法: using DocumentLanguageDictionary = System.Collections.Generic
Dictionary<Tuple<int, int, int>, Document>
字典
字典包含如下信息:
现在我需要把它们做成这样的形式:
括号之间的数字是id。
因此,我需要在相应的ID组合下显示文档
我的文档标题应显示在“文档”列中。
在相应的语言下,我想要一个“X”。这就是我们解决问题的方法:
using DocumentLanguageDictionary = System.Collections.Generic.Dictionary<int, CustomerPortalDomain.Entities.Document>;
using DocumentList = System.Collections.Generic.List<System.Collections.Generic.Dictionary<int, CustomerPortalDomain.Entities.Document>>;
using DocumentTypeDictionary = System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<System.Collections.Generic.Dictionary<int, CustomerPortalDomain.Entities.Document>>>;
using BusinessUnitDictionary = System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<System.Collections.Generic.Dictionary<int, CustomerPortalDomain.Entities.Document>>>>;
private BusinessUnitDictionary CreateMatrix(UserPermission userPermission)
{
BusinessUnitDictionary matrix = new BusinessUnitDictionary();
var documentList = userPermission.Documents.GroupBy(x => new { Title = x.Title, BusinessUnitId = x.BusinessUnit.Id, DocumentTypeId = x.DocumentType.Id }, (key, group) => new { key = key, translations = group.ToList() }).ToList();
foreach (var document in documentList)
{
int businessUnitId = document.key.BusinessUnitId;
if (!matrix.ContainsKey(businessUnitId))
{
matrix[businessUnitId] = new DocumentTypeDictionary();
}
DocumentTypeDictionary documentDictionary = matrix[businessUnitId];
int documentType = document.key.DocumentTypeId;
if (!documentDictionary.ContainsKey(documentType))
{
documentDictionary[documentType] = new DocumentList();
}
DocumentList list = documentDictionary[documentType];
DocumentLanguageDictionary languageDictionary = new DocumentLanguageDictionary();
foreach (var translation in document.translations)
{
languageDictionary[translation.Language.Id] = translation;
}
list.Add(languageDictionary);
}
return matrix;
}
使用DocumentLanguageDictionary=System.Collections.Generic.Dictionary;
使用DocumentList=System.Collections.Generic.List;
使用DocumentTypeDictionary=System.Collections.Generic.Dictionary;
使用BusinessUnitDictionary=System.Collections.Generic.Dictionary;
private BusinessUnitDictionary CreateMatrix(用户权限用户权限)
{
BusinessUnitDictionary矩阵=新建BusinessUnitDictionary();
var documentList=userPermission.Documents.GroupBy(x=>new{Title=x.Title,BusinessUnitId=x.BusinessUnit.Id,DocumentTypeId=x.DocumentType.Id},(key,group)=>new{key=key,translations=group.ToList()).ToList();
foreach(文档列表中的var文档)
{
int businessUnitId=document.key.businessUnitId;
如果(!matrix.ContainsKey(businessUnitId))
{
矩阵[businessUnitId]=新文档类型字典();
}
DocumentTypeDictionary documentDictionary=矩阵[businessUnitId];
int documentType=document.key.DocumentTypeId;
如果(!documentDictionary.ContainsKey(documentType))
{
documentDictionary[documentType]=新文档列表();
}
DocumentList list=documentDictionary[documentType];
DocumentLanguageDictionary languageDictionary=新的DocumentLanguageDictionary();
foreach(文档中的var翻译。翻译)
{
languageDictionary[translation.Language.Id]=翻译;
}
添加(语言词典);
}
收益矩阵;
}
然后在视图中,我们可以循环并以正确的格式显示数据。在我的元组中:第一个数字是BusinessUnit(即HPP)。第二个数字是DocumentType(即TDS)。第三个数字是语言(即JP)