Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I do a LINQ GroupBy where the key can be reversed?
    primarykey
    data
    text
    <p>I am trying to do a complicated GroupBy in LINQ, but I am having trouble with my key selector. In the following code, I am able to group by my key in one direction (by SellerID, BuyerID), but I actually need to group by my key in the inverse as well (by SellerID, BuyerID or BuyerID, SellerID). My ultimate goal of this query is that when the keys are reversed, I need to make the Asset amount negative. This will allow me to net out any amounts that exist on both sides and then I will end up with only Records that have amounts on that particular side.</p> <p>The following code should explain it:</p> <pre><code>public class Record { public int RecordID; public int SellerID; public int BuyerID; public List&lt;Asset&gt; Assets; } public class Asset { public int AssetID; public decimal Amount; } var groups = new List&lt;Record&gt; { new Record { RecordID = 1, SellerID = 100, BuyerID = 200, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 5, Amount = 10 }}}, new Record { RecordID = 2, SellerID = 100, BuyerID = 200, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 5, Amount = 20 }}}, new Record { RecordID = 3, SellerID = 100, BuyerID = 200, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 6, Amount = 60 }}}, new Record { RecordID = 4, SellerID = 200, BuyerID = 100, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 5, Amount = 40 }}}, new Record { RecordID = 5, SellerID = 200, BuyerID = 100, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 5, Amount = 50 }}}, new Record { RecordID = 6, SellerID = 200, BuyerID = 100, Assets = new List&lt;Asset&gt; { new Asset { AssetID = 6, Amount = 35 }}} }; var result = groups.GroupBy( r =&gt; new { r.SellerID, r.BuyerID }, r =&gt; r.Assets, (r, assets) =&gt; new { r.SellerID, r.BuyerID, AssetSummation = assets.SelectMany(asset =&gt; asset).GroupBy(a =&gt; a.AssetID).Select(a2 =&gt; new { AssetID = a2.Key, Amount = a2.Sum(a3 =&gt; a3.Amount) }) }); </code></pre> <p>What I would like my output to be is the following: </p> <ul> <li>Record 1 <ul> <li>Seller: 100</li> <li>Buyer: 200</li> <li>Assets: <ul> <li>Asset <ul> <li>AssetID: 6</li> <li>Amount: 25</li> </ul></li> </ul></li> </ul></li> <li>Record 2 <ul> <li>Seller: 200</li> <li>Buyer: 100</li> <li>Assets: <ul> <li>AssetID: 5</li> <li>Amount: 60</li> </ul></li> </ul></li> </ul> <p>I think I have a good start, but I am not sure where to go from here. How do I flip the key and then make the amounts negative, so I can sum them up? I think that after I am able to do that, I can filter out any Asset rows where the value is 0 (meaning that the record was fulfilled by the inverse.</p> <p>EDIT #1: Maybe what I am trying to do would be to join the groups variable to itself to SUM all matching records on both sides of the join. So I would end up joining the SellerID on the left side to the BuyerID on the right side and the BuyerID on the left side to the SellerID on the right side. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload