LINQ JOIN + GROUP BY + SUM

I have two LINQ statements that I would like to do in one, but for life I can't get it to work.

I can not get the group to work in the first expression. He complains that there are no TotalBuy and TotalSell properties, although he does not complain about AmountTC and AmountAUD .

It should be easy. Any thoughts?

 var itineraryItems = from ii in this.ItineraryItemRecords join t in this.TransactionRecords on ii.OperatorID equals t. TransactionActor.OperatorID into g select new { OperatorID = ii.OperatorID, TotalBuy = g.Sum(i = >ii.TotalBuy) , TotalSell = g.Sum(i = >ii.TotalSell) , PaidTC = (0 - (g.Sum(t = >t.AmountTC))) , PaidAUD = (0 - (g.Sum(t = >t.AmountAUD))) }; var itineraryItemz = from i in itineraryItems group i by i.OperatorID into g select new { OperatorID = g.Key, TotalBuy = g.Sum(i = >i.TotalBuy) , TotalSell = g.Sum(i = >i.TotalSell) , PaidTC = (0 - (g.Sum(i = >i.PaidTC))) , PaidAUD = (0 - (g.Sum(i = >i.PaidAUD))) }; 

As a side note, ItineraryItemRecords and TransactionRecords are collections of classes handled by SubSonic .

It really should be easy, so any help would be appreciated.

Regards, John

+4
source share
1 answer

A small bug fixed:

 var itineraryItems = from ii in this.ItineraryItemRecords join t in this.TransactionRecords on ii.OperatorID equals t.TransactionActor.OperatorID into g select new { OperatorID = ii.OperatorID //, TotalBuy = g.Sum(i => ii.TotalBuy) , TotalBuy = g.Sum(i => i.TotalBuy) //, TotalSell = g.Sum(i => ii.TotalSell) , TotalSell = g.Sum(i => i.TotalSell) , PaidTC = (0 - (g.Sum(t => t.AmountTC))) , PaidAUD = (0 - (g.Sum(t => t.AmountAUD))) }; 

I recommend against reusing identifiers - this will help avoid these errors in the future.

+10
source

Source: https://habr.com/ru/post/1301992/


All Articles