4

我正在尝试在 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) })
    });

我希望我的输出如下:

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

我想我有一个好的开始,但我不知道从这里去哪里。我如何翻转钥匙然后使金额为负数,以便我可以总结它们?我认为,在我能够做到这一点之后,我可以过滤掉任何值为 0 的资产行(这意味着记录是由逆向实现的。

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

4

2 回答 2

1

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

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) });

这个怎么运作?非常简单:

  1. 对于每条记录,我选择两条记录 - 一条原样,另一条带有反向卖方和买方(所有资产的金额均为负数)。然后我用 . 展平所有记录SelectMany
  2. 并按卖方和买方分组。
  3. 其余是对每个组的资产数量的简单计算。

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

于 2013-03-18T19:43:42.523 回答
0

助手类:

public class RecordItem
{
    public int SellerID;
    public int BuyerID;
    public int AssetID;
    public decimal Amount;
}

平等比较器:

public class RecordItemEqualityComparer : IEqualityComparer<RecordItem>
{
    public bool Equals(RecordItem x, RecordItem y)
    {
        if (x.AssetID != y.AssetID)
            return false;

        if (x.BuyerID == y.BuyerID && x.SellerID == y.SellerID)
            return true;

        if (x.BuyerID == y.SellerID && x.SellerID == y.BuyerID)
            return true;

        return false;
    }

    public int GetHashCode(RecordItem obj)
    {
        return string.Format("{0}_{1}", obj.BuyerID * obj.SellerID, obj.AssetID).GetHashCode();
    }
}

和 LINQ 查询:

var recordItemComparer = new RecordItemEqualityComparer();

var items = groups.SelectMany(r => r.Assets.Select(a => new RecordItem {
                                                            BuyerID = r.BuyerID,
                                                            SellerID = r.SellerID,
                                                            AssetID =a.AssetID,
                                                            Amount = a.Amount
                                                        }))
                  .GroupBy(ri => ri, recordItemComparer)
                  .Select(g => new RecordItem() {
                        BuyerID = g.Key.BuyerID,
                        SellerID = g.Key.SellerID,
                        AssetID = g.Key.AssetID,
                        Amount = g.Sum(ri => (ri.BuyerID == g.Key.BuyerID) ? ri.Amount : -1 * ri.Amount)
                  }).ToList();

返回您想要的:包含 2 个项目的列表。总和计算正确,但是买卖双方顺序可能不正确,因此总和可能是 eg-60而不是60

PS。这是一个非常好的挑战!

于 2013-03-18T19:13:56.573 回答