Reputation: 31
I have the following code to group payment amount by Invoice Number and
sum(Amount)
as Interest
,sum(Amount)
as Principal
I tried the following code to Group by InvoiceNumber
as shown below but i get InvalidOperationException: The LINQ expression GroupByShaperExpression'
exception.
I want to accomplish
Type Amount InvoiceNumber
1 $100 123
2 $50 123
3 $100 123
4 $1200 123
1 $100 124
1 $300 124
3 $100 124
3 $300 124
4 $100 124
I want to group by invoice Number and Sum the value of Amount field for Type = 1&2 and display as Interest and 3&4 as Princepal
InvoiceNumber Interest Princepal
123 $150 $1300
124 $400 $500
Upvotes: 1
Views: 77
Reputation: 27526
There are several problems:
GroupBy
Consider to rewrite the query in the following way:
var ProductPaymentIDs = ProductPayments.Select(pp => pp.ID).ToList();
var query =
from cl in Context.PaymentCodingLines
where ProductPaymentIDs.Contains(cl.ProductPaymentID)
group new { cl.InvoiceLineNav.TypeID, cl.Amount } by g.InvoiceLineNav.InvoiceNav.InvoiceNumber into g
select new DetailDTO
{
InvoiceNumber = g.Key,
InterestPaid = g.Sum(x => x.TypeID == 1 || x.TypeID == 2 ? x.Amount : 0),
PrincipalPaid = g.Sum(x => x.TypeID == 3 || x.TypeID == 4 ? x.Amount : 0)
};
var Details = query.ToList();
Upvotes: 1