如何执行可以反转密钥的 LINQ GroupBy?

Jon*_*Jon 5 c# linq

我正在尝试在 LINQ 中执行复杂的 GroupBy,但我的键选择器遇到问题。在下面的代码中,我可以按一个方向的密钥进行分组(按 SellerID、BuyerID),但实际上我还需要按相反的密钥进行分组(按 SellerID、BuyerID 或 BuyerID、SellerID)。我这个查询的最终目标是当密钥反转时,我需要将资产金额设置为负数。这将使我能够扣除双方存在的任何金额,然后我最终将只得到该特定一方有金额的记录。

下面的代码应该解释它:

public class Record
{
    public int RecordID;
    public int SellerID;
    public int BuyerID;
    public List<Asset> Assets;
}

public class Asset
{
    public int AssetID;
    public decimal Amount;
}

var groups = new List<Record>
{
    new Record { RecordID = 1, SellerID = 100, BuyerID = 200, Assets = new List<Asset> { new Asset { AssetID = 5, Amount = 10 }}},
    new Record { RecordID = 2, SellerID = 100, BuyerID = 200, Assets = new List<Asset> { new Asset { AssetID = 5, Amount = 20 }}},
    new Record { RecordID = 3, SellerID = 100, BuyerID = 200, Assets = new List<Asset> { new Asset { AssetID = 6, Amount = 60 }}},
    new Record { RecordID = 4, SellerID = 200, BuyerID = 100, Assets = new List<Asset> { new Asset { AssetID = 5, Amount = 40 }}},
    new Record { RecordID = 5, SellerID = 200, BuyerID = 100, Assets = new List<Asset> { new Asset { AssetID = 5, Amount = 50 }}},
    new Record { RecordID = 6, SellerID = 200, BuyerID = 100, Assets = new List<Asset> { new Asset { AssetID = 6, Amount = 35 }}}
};

var result = groups.GroupBy(
    r => new { r.SellerID, r.BuyerID },
    r => r.Assets,
    (r, assets) => new
    {
        r.SellerID,
        r.BuyerID,
        AssetSummation = assets.SelectMany(asset => asset).GroupBy(a => a.AssetID).Select(a2 => new { AssetID = a2.Key, Amount = a2.Sum(a3 => a3.Amount) })
    });
Run Code Online (Sandbox Code Playgroud)

我希望我的输出如下:

  • 记录1
    • 卖家:100
    • 买家:200
    • 资产:
      • 资产
        • 资产ID:6
        • 数量:25
  • 记录2
    • 卖家:200
    • 买家:100
    • 资产:
      • 资产ID:5
      • 数量:60

我认为我有一个良好的开始,但我不知道接下来该何去何从。如何翻转钥匙,然后将金额变为负数,以便我可以将它们相加?我认为,在我能够做到这一点之后,我可以过滤掉值为 0 的任何资产行(意味着该记录是通过逆来实现的。

编辑#1:也许我想要做的是将 groups 变量连接到其自身,以对连接两侧的所有匹配记录进行求和。因此,我最终会将左侧的 SellerID 连接到右侧的 BuyerID,将左侧的 BuyerID 连接到右侧的 SellerID。

Ser*_*kiy 2

这是返回您预期结果的查询:

var result = records
    .SelectMany(r => new[] { r, new Record { // 1
           SellerID = r.BuyerID, 
           BuyerID = r.SellerID, 
           Assets = r.Assets.Select(a => new Asset { 
                        AssetID = a.AssetID, 
                        Amount = -a.Amount
                    }).ToList() }})
    .GroupBy(r => new { r.SellerID, r.BuyerID }) // 2
    .Select(g => new { // 3
           Seller = g.Key.SellerID,
           Buyer = g.Key.BuyerID,
           Assets = g.SelectMany(r => r.Assets)
                     .GroupBy(a => a.AssetID)
                     .Select(ag => new { 
                         AssetID = ag.Key,
                         Amount = ag.Sum(a => a.Amount) })
                     .Where(x => x.Amount > 0) });
Run Code Online (Sandbox Code Playgroud)

怎么运行的?很简单的:

  1. 对于每条记录,我选择两条记录 - 一条按原样,另一条具有反向的卖方和买方(而且所有资产都有负金额)。然后我用 展平所有记录SelectMany
  2. 并按卖家和买家对它们进行分组。
  3. 剩下的就是简单计算各组的资产金额。

顺便说一句,您可以在最后一个 select 语句中创建Record和对象,而不是返回匿名对象。Asset