0

以下のようなタイプの辞書オブジェクトがあります。

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


            }
        }
    }
}
`

この順序で並べ替えるにはどうすればよいですか?.NET2.0フレームワークの使用に制限されています。

4

2 に答える 2

1

辞書はソートできません。それらはリストではありません。さらに:

あなたの構造は悪いです-Dictionary同じキーを複数回含めることはできないため、提供しているサンプルを作成することさえできません:

"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」を複数回含めることはできません。「内部ディクショナリ」には、ロールをApprover複数回含めることはできず、最後のレベルでも、Period.Past/Ahead複数回キーにすることはできません。

List<T>代わりに構造をT適切なデータ構造に変更するか、他の人がすでに指摘したように、型付きデータセットに変更して、構造をテーブルのように扱います。

編集
私は今、私の答えを編集しています.

私が言っているのは、辞書が同じキーを 2 回持つことはあり得ないということです。したがって、このルールに従って、ケースを次のように減らす必要があります。

"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products

ルールに適用される何かについて話しているので、「並べ替えの方法は?」と尋ねることができます。答え: できません。ディクショナリは、定義上、順序付けられていない構造です。ただし、特定の順序で値を取得するようにすることはできます。Past製品が常に製品の前に来るように製品を「並べ替える」場合は、Ahead最初にそれぞれのキーを使用するようにしてください。

編集2

これはコピー/貼り付けエラーに基づいていることに気付きました。あなたが話しているデータは次のようになります。

"Sachin" --> Roles.Preparer --> Period.Ahead --> Products
"Sachin" --> Roles.Preparer --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Past --> Products
"Sachin" --> Roles.Approver --> Period.Ahead --> Products

このコードを使用してアイテムを追加していると言いました:

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

次に、同様のコードを使用してアイテムを取得できます。eMail-ID を指定すると、次のようにすると、「承認者の前の作成者」および「前者の前」の順序でアイテムのリストが取得されます。

List<Product> productsForEMailID = new List<Product>();

productsForEMailID.AddRange(dict[emailID][Roles.Preparer][Period.Past]);
productsForEMailID.AddRange(dict[emailID][Roles.Approver][Period.Past]);
productsForEMailID.AddRange(dict[emailID][Roles.Preparer][Period.Ahead]);
productsForEMailID.AddRange(dict[emailID][Roles.Approver][Period.Ahead]);

製品のリストは「ソート」されています。

于 2013-01-21T10:12:33.207 に答える
0

この問題により適した別のタイプのキーを使用できます。簡単なハックが必要な場合SortedDictionary<string, List<Product>>

"Sachin#0#0" --> Products
"Sachin#0#1" --> Products
...

ここでは、名前に「#」文字を含めることはできないと想定しています。最初の数字は、、を表し、Roles2番目の数字は、を表します。Preparer = 0Approver = 1PeriodAhead = 0Past = 1

または-もう少し堅牢なソリューションが必要な場合は、次のようにします。

public struct Key : IComparable<Key>
{
    public String Name;
    public Roles Role;
    public Period Period;

    public int CompareTo(Key other)
    {
        var c = String.Compare(Name, other.Name, StringComparison.Ordinal);
        if (c != 0) return c;
        c = Role.CompareTo(other.Role);
        if (c != 0) return c;
        return Period.CompareTo(other.Period);
    }
}

...そしてを使用しSortedDictionary<Key, List<Product>>ます。

于 2013-01-21T10:38:17.287 に答える