1

私はまだLINQを理解しようとしています.単純な関数については少し理解していますが、これらを積み重ねることはある程度できません.誰かが私に手を貸してくれることを願っています.

私は現在、効果的な辞書を持っています:

Dictionary<Type, Dictionary<int, EntityComponent>> ComponentSystems

特定のキーを持つ EntityComponent のすべてのインスタンスを返すメソッドを作成しようとしています。つまり、親ディクショナリに 2 つのネストされたディクショナリが含まれ、それぞれにキーとして '1' を持つエントリがある場合、メソッドは上記のキーに一致する値をIEnumerable<EntityComponent>

私はSelectMany他のさまざまな LINQ コマンドを使いこなしてきましたが、まだそれを理解するのに苦労しています。

明確にするための別の例として、次の設定があるとしましょう。

Dictionary<object, Dictionary<int, string>> test = 
   new Dictionary<object, Dictionary<int, string>>();

Dictionary<int, string> test1 = new Dictionary<int, string>();
test1[1] = "test1-1";
test1[2] = "test1-2";
test[0] = test1;

Dictionary<int, string> test2 = new Dictionary<int, string>();
test2[1] = "test2-1";
test2[2] = "test2-2";
test[1] = test2;

探しているキーが「1」であるとすると、「test1-1」と「test2-1」の両方を返す linq ステートメントを作成する必要があります。

4

1 に答える 1

2
int key = 1;
var query = test.Values // select inner dictionaries
                .Where(d => d.ContainsKey(key)) // which contain your key
                .Select(d => d[key]); // and return value by key

戻り値:

"test1-1"
"test2-1"
于 2013-07-05T20:53:01.390 に答える