Reputation: 10153
I have the following information
var details = Details.Where(d => d.isActive);
I would like to query another table, Authorizations
, that has a FK to Details
, and get the sum amounts of the authorizations that are contained within the details
object grouped by the detail
and a FundCode
.
Details (1 to many) Authorizations
Seems simple enough, however I'm having a bit of trouble.
Here is what I currently have:
var account = (from sumOfAuths in Authorizations
where details.Contains(sumOfAuths.Details)
&& sumOfAuths.RequestStatusId == 2
group sumOfAuths by new { sumOfAuths.Detail, sumOfAuths.FundCode } into child
select new {
....
Amount = child.Amount
}).Sum()
The problem is that inside the .Sum()
function I have collection of objects rather than 1. So I can't Sum the amounts properly.
Upvotes: 2
Views: 125
Reputation: 2203
I believe this query produces what you're looking for:
var account = from c in Authorizations
where details.Contains(c.Details) && c.RequestStatusId == 2
group c by new { c.Detail, c.FundCode } into g
select new { Key = g.Key, Sum = g.Sum(x => x.Amount) };
Upvotes: 2
Reputation: 129792
Generally, you can specify what it is you want to sum:
.Sum(x => x.Amount)
In groups, you can use nested sums:
.Sum(x => x.Sum(y => y.Amount));
Upvotes: 3