Linq query problem

I have the following information

var details = Details.Where(d => d.isActive);

      

I would like to query another table Authorizations

that has FK before Details

, and get the sums of the sums of the permissions that are contained in the object Details

, grouped by detail

and a FundCode

.

Details (1 to many)

Seems pretty simple, however I have a bit of a problem.

Here's 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 function .Sum()

I have a set of objects, not 1. So I cannot sum the sums correctly.

+2


a source to share


2 answers


I believe this query produces what you are 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) };

      

+2


a source


Typically, you can specify what you want to sum:

.Sum(x => x.Amount)

      



In groups, you can use nested amounts:

.Sum(x => x.Sum(y => y.Amount));

      

+3


a source







All Articles