1

it is possible to perform a LINQ like this SQL:

select invoice.credit, ((sum(detailsInvoice.amount) - sum(detailsInvoice.discount))+ sum(detailsInvoice.tax)) as total
from detailsInvoice
join invoice on detailsInvoice.invoiceID = invoice.ID
where invoice.carga = 1428 and invoice.ID <> 0
group by invoice.credit

I'm getting this result in the SQL

enter image description here

I spend so much time trying to create a LINQ to perform that query, without luck.

DanielRamiz
  • 149
  • 10

2 Answers2

1

you should do something like this, please excuse any typo errors, didn't use a compiler to test it.

var result = db.invoice.Include(x=>x.detailsInvoice).
                               GroupBy(x=>invoice.credit).
                               Select(y=> new {  
                                credit = y.Key,
                                Total =  (y.Sum(z=>z.detailsInvoice.amount) - y.Sum(z=>z.detailsInvoice.discount) + y.Sum(z=>detailsInvoice.tax))
                                });

Hope this helps!

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42
0

your answer gave me a good idea of what to do. something important I forgot to mention is the Invoice table and detailsInvoice don't have a relationship.

So this is what I did:

var result = (from _invoice in ruterosContext.Invoice
                join _details in ruterosContext.DetailsInvoice
                on new { id = _invoice.ID, reference = _invoice.Reference } equals 
                new { id = _details.invoiceID, reference = _details.Reference }   
                where _invoice.ID != 0 && _invoice.Carga == 1428
                select new {
                    Credit = _invoice.credit,
                    amount = _details.amount,
                    discount = _details.discount, 
                    tax = _details.tax
                }).GroupBy(x => x.credit).
                     Select(y => new { Credit = y.Key, 
                     Total = (y.Sum(z => z.amount) - y.Sum(z => z.discount)) + y.Sum(x => x.tax) });
DanielRamiz
  • 149
  • 10