1

こんにちは、私は 2 つの辞書を持っています。それらを結合する方法を見つける必要があります。2 つの辞書タイプを次に示します。

IDictionary<string, byte[]> dictionary1
IDictionary<IFileShareDocument, string> dictionary2

この 2 つの辞書から、次のような 3 つ目の辞書を作成する必要があります。

IDictionary<IFileShareDocument, byte[]> dictionary3

両方の辞書にはまったく同じ数の項目があり、両方の文字列プロパティがリンク ポイントです。

私が望むのは、次のようなことをする何かを書くことができることです:

dictionary1.value join with dictionary2.key
where dictionary1.key == dictionary2.value

This statement should result in dictionary3.

これを行う方法を見つけることができないように見えるこれを達成する方法はありますか?

4

3 に答える 3

5
var dictionary3 =
    dictionary1
        .Join(dictionary2, x => x.Key, x => x.Value, (x, y) => new { x, y })
        .ToDictionary(a => a.y.Key, a => a.x.Value);
于 2013-08-28T14:11:53.190 に答える
2

LINQクエリ構文を使用してそれを行う方法を次に示しますjoin(これは@KingKingのソリューションとほぼ同じものにコンパイルされます):

IDictionary<IFileShareDocument, byte[]> dictionary3 =
    (from item1 in dictionary1
     join item2 in dictionary2 on item1.Key equals item2.Value
     select new { item2.Key, item1.Value })
         .ToDictionary(x => x.Key, x => x.Value);

上記は、より効率的であるため、fromandを使用したこの例よりもはるかに好ましいことに注意してください。whereあなたが私のようであれば(このようなものを自動的に結合に変換するSQLに精通している)、この貧弱な方法が最初に思い浮かぶかもしれないので、ここにこれを含めます:

IDictionary<IFileShareDocument, byte[]> dictionary3 =
    (from item1 in dictionary1
     from item2 in dictionary2
     where item1.Key == item2.Value
     select new { item2.Key, item1.Value })
         .ToDictionary(x => x.Key, x => x.Value);
于 2013-08-28T14:15:10.847 に答える
2

これはうまくいきますか?

var result =
    dictionary2
        .ToDictionary(x => x.Key, x => dictionary1[x.Value]);
于 2013-08-28T14:29:50.083 に答える