Linq到对象嵌套分组

时间:2009-07-07 02:13:44

标签: linq group-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)));

这是问题所在。如果我在查询中将“Target”设置为“target1”,那么大部分结果都是我所期望的:

txc1
   bc1
      target1/v1 
      target1/v2
   bc3
      target1/v5

这是一个好的开始,但我也得到了:

txc2
txc3

添加到列表中以获得完整的结果,如下所示:

txc1
   bc1
      target1/v1 
      target1/v2
   bc3
      target1/v5
txc2
txc3

如果与“目标”匹配,我希望查询仅返回顶级组。在上面的例子中,它仍然将“txc2”和“txc3”作为顶级组返回,即使它们与“目标”不匹配。

我希望这不会太混乱。有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

将您的Where子句复制到GroupBy之外。

var history = transactionLog.Where(record => record.TargetName == "target1")
    .GroupBy(tx => tx.txSetComments)
    .ToDictionary(
        g => g.Key,
        g => g.GroupBy(b => b.BatchComments)
               .ToDictionary(e => e.Key,
                             e => e.Where(t => t.TargetName == "target1"));