如何将我的字典转换为矩阵?
How can I transform my Dictionary to a Matrix?
我有这本字典,其定义如下:
Dictionary<Tuple<int, int, int>, Document>
字典包含这样的信息:
现在我需要以这样的形式获取它们:
括号内的数字为id。
所以我需要在对应的ID组合下显示文档。
我的文档标题应该显示在 "Documents" 栏中。
在相应的语言下,我想要一个 "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;
}
然后在视图中我们可以循环并以正确的格式显示数据。
我有这本字典,其定义如下:
Dictionary<Tuple<int, int, int>, Document>
字典包含这样的信息:
现在我需要以这样的形式获取它们:
括号内的数字为id。 所以我需要在对应的ID组合下显示文档。
我的文档标题应该显示在 "Documents" 栏中。 在相应的语言下,我想要一个 "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;
}
然后在视图中我们可以循环并以正确的格式显示数据。