如何将字典转换为矩阵?

时间:2015-02-25 10:23:03

标签: dictionary matrix asp.net-mvc-5 tuples

我有这个字典,其定义如下:

Dictionary<Tuple<int, int, int>, Document>

字典包含以下信息:

enter image description here

现在我需要以这样的形式获取它们:

enter image description here

括号内的数字是id。 所以我需要在ID的相应组合下显示文档。

我的文档标题应显示在“文档”列中。 在相应的语言下,我想要一个“X”。

1 个答案:

答案 0 :(得分:0)

这就是我们解决问题的方法:

    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;
    }

然后在视图中我们可以以正确的格式循环和显示数据。

相关问题