我有一个字典对象,其类型如下。
Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>>
以“准备者”和“批准者”为项目的角色(是一个枚举)。同样 Period 是另一个具有“Ahead”和“Past”项目的枚举。
该列表包含各种产品的列表。
我在字典中有以下层次结构中的项目。
"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products
我将不得不按以下顺序对字典进行排序。
"Sachin" --> Roles.Preparer --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products
这种结构是必需的,因为我必须遍历每个项目并且应该将其添加为邮件的一部分。
实际代码是这样的。
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Basics
{
class Product
{
public string Name { get; set; }
public int Days { get; set; }
}
enum Period
{
Ahead,
Past
}
enum Roles
{
Preparer,
Approver
}
class Program
{
static void Main(string[] args)
{
DictionaryProcessing(new string[] { "sriram123@yahoo.com", "abhishek321@yahoo.com" });
}
private static void DictionaryProcessing(string[] emailIDs)
{
List<Product> products = new List<Product>();
Product product1 = new Product() { Name = "Pencil", Days = 14 };
Product product2 = new Product() { Name = "Eraser", Days = 2 };
Product product3 = new Product() { Name = "Geometry Box", Days = 31 };
products.Add(product1);
products.Add(product2);
products.Add(product3);
Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>> dict = new Dictionary<string, Dictionary<Roles, Dictionary<Period, List<Product>>>>();
///
foreach (string emailID in emailIDs)
{
if (!dict.ContainsKey(emailID))
dict.Add(emailID, new Dictionary<Roles, Dictionary<Period, List<Product>>>());
if (!dict[emailID].ContainsKey(Roles.Preparer))
dict[emailID].Add(Roles.Preparer, new Dictionary<Period, List<Product>>());
if (!dict[emailID][Roles.Preparer].ContainsKey(Period.Ahead))
dict[emailID][Roles.Preparer].Add(Period.Ahead, new List<Product>());
if (!dict[emailID][Roles.Preparer].ContainsKey(Period.Past))
dict[emailID][Roles.Preparer].Add(Period.Past, new List<Product>());
///
if (!dict[emailID].ContainsKey(Roles.Approver))
dict[emailID].Add(Roles.Approver, new Dictionary<Period, List<Product>>());
if (!dict[emailID][Roles.Approver].ContainsKey(Period.Ahead))
dict[emailID][Roles.Approver].Add(Period.Ahead, new List<Product>());
if (!dict[emailID][Roles.Approver].ContainsKey(Period.Past))
dict[emailID][Roles.Approver].Add(Period.Past, new List<Product>());
for (int i = 0; i < products.Count; i++)
{
dict[emailID][Roles.Preparer][Period.Ahead].Add(products[i]);
dict[emailID][Roles.Preparer][Period.Past].Add(products[i]);
dict[emailID][Roles.Approver][Period.Past].Add(products[i]);
dict[emailID][Roles.Approver][Period.Ahead].Add(products[i]);
}
}
}
}
}
`
如何按此顺序对其进行排序?我仅限于使用 .NET 2.0 框架。