It is not very clear why you want to do a left join. I have assumed that it is because there are invoices that simply do not have assigned movements, so the following fragment of code revolves around that.
var q = from fac in db.Facturas
join mov in db.MovimientosDineros
on fac.IDFactura equals mov.IdFactura into facturasMovimientos // 1
from facMov in facturasMovimientos.DefaultIfEmpty() // 2
group facMov by fac into grupo // 3
let sum = grupo.Sum(g => g == null ? 0 : g.Valor) // 4
select new
{
IDFactura = grupo.key.IDFactura,
Totalaportado = sum
};
I will explain the code using the numbers I have indicated in the comments:
- In 1 what has been done is to join the invoices with their movements and has "englobado" this union in a table invoices Movements.
- In 2 we specify that if an invoice has no movements, a default value (null) is returned. Consult the documentation of this function if you see it necessary.
Summary: Returns the elements of the specified sequence or the
default value of the type parameter in a singleton collection if the > sequence is empty.
- In 3 we group all rows of invoices Movements by invoices.
- In 4 the total sum is calculated specifying that if any row is null the summand is 0.
Explained the code, I have to say that I have not been able to prove it, so if it contains any errors or my concepts are wrong, indicate it and I will correct it, please.