1
私はこのクエリを動作させていますが、私が探しているものを正確に返していません。オブジェクトへのLinqのネストグループby
私がのコレクションを持っている:
List<TransactionRecord> transactionLog;
TransactionRecord簡素化が次のようになります。
class TransactionRecord {
public string txSetComments;
public string BatchComments;
public string TargetComments;
public string TargetName;
public string TargetValue;
}
とのように初期化されることがあります。ここでは
List<TransactionRecord> transactionLog = new List<TransactionRecord>()
{
new TransactionRecord { txSetComments = "txc1",
BatchComments = "bc1",
TargetComments = "tc1",
TargetName = "target1",
TargetValue = "v1" },
new TransactionRecord { txSetComments = "txc1",
BatchComments = "bc1",
TargetComments = "tc1",
TargetName = "target1",
TargetValue = "v2" },
new TransactionRecord { txSetComments = "txc2",
BatchComments = "bc2",
TargetComments = "tc1",
TargetName = "target2",
TargetValue = "v3" },
new TransactionRecord { txSetComments = "txc2",
BatchComments = "bc1",
TargetComments = "tc1",
TargetName = "target2",
TargetValue = "v4" },
new TransactionRecord { txSetComments = "txc1",
BatchComments = "bc3",
TargetComments = "tc1",
TargetName = "target1",
TargetValue = "v5" },
new TransactionRecord { txSetComments = "txc3",
BatchComments = "bc3",
TargetComments = "tc1",
TargetName = "target3",
TargetValue = "v6" }
};
は、これまでのクエリです:
Dictionary<string, Dictionary<string, IEnumerable<TransactionRecord>>> history =
transactionLog.GroupBy(tx => tx.TxSetComments)
.ToDictionary(g => g.Key,
g => g.GroupBy(b => b.BatchComments).ToDictionary(e => e.Key,
e => e.Where(t => t.TargetName == Target)));
ここに問題があります。私は「ターゲット1」へのクエリで「ターゲット」を設定した場合、結果のほとんどは、私が期待通りである:
txc1
bc1
target1/v1
target1/v2
bc3
target1/v5
これは良いスタートですが、私はまた、取得:
追加txc2
txc3
以下のように好きに見える完全な結果のためのリストに:
txc1
bc1
target1/v1
target1/v2
bc3
target1/v5
txc2
txc3
私は「ターゲット」との一致がある場合、トップレベルのグループを返すだけにクエリをしたいと思います。上記の例では、 "Target"と一致しないにもかかわらず "txc2"と "txc3"をトップレベルのグループとして返します。
私はこれがあまりにも混乱しないことを望みます。どんな勧告?
ありがとうございます!
ああ、今、それはとても明らかです。それは私が試していなかったものです。数時間にわたり入れ子にされたlinqクエリを見つめた後、盲目的になる方法を驚かせます。 ありがとう! – IUnknown