English 中文(简体)
Linq - 2 tables - Sum
原标题:

Asiento ******** Id_Asiento integer key Fecha date

It_Asiento
**********
Id_Asiento integer Forenkey
Importe float

I wan to do This SQL Query with Linq

select Asiento.Id_Asiento, Asiento.Fecha, Sum(It_Asiento.Importe) 
From Asiento 
join It_Asiento 
on Asiento.Id_Asiento = It_Asiento.Id_Asiento
and It_Asiento.Importe > 0
group by Asiento.Id_Asiento, Asiento.Fecha

On my DataBase boot tables have a relation, so on C# / Linq i don t need to make a join, right??

I try this, but don t work

IQueryable listAsientos = from it_a in dc_Asientos.It_Asientos
                          where it_a.importe > 0
                          group it_a by it_a.id_asto, it_a.Asiento.fecha
                          **//¿it_a.ASiento.Fecha??????**
                          into resultado
                          select new
                          { 
                              id_asto = resultado.Key,
                              **/¿¿¿¿¿fecha = it.a.Asiento.Fecha?????**
                              suma = resultado.Sum(it => it.importe)
                          };

Can somebody help me? SORRY for my poor English.

问题回答

Isn;t resultado an It_Asientos object? So you should use

id_asto = resultado.Key.Id_asto,
fecha = resultado.Key.fecha

The following query may solve your problem

IQueryable listAsientos = from it_a in dc_Asientos.It_Asientos
                      where it_a.importe > 0
                      group it_a by new { it_a.id_asto, it_a.Asiento.fecha }
                      into resultado
                      select new
                      { 
                          id_asto = resultado.Key.id_asto,
                          suma = resultado.Sum(it => it.importe)
                      };




相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签