2013-04-18 15 views
0

這是我的sql命令。先進的實體框架查詢示例

select c.nombrecompleto fullname,c.direccion street ,c.telefono phonenumber, 
c.limitecredito creditlimit,sum(vd.preciofinal) balance , case when max(a.fechacreacion) is null then '' else max(a.fechacreacion) end 
lastpay 
, 
case when u.nombre is null then '' else u.nombre end paidTo 
from cliente c 
inner join venta v on c.idcliente=v.idcliente 
inner join ventadetalle vd on v.idventa=vd.idventa 
inner join prestamo p on p.idventa=v.idventa 
left join abono a on c.idcliente=a.idcliente 
left join usuario u on a.creadopor=u.idusuario 
where 
c.estatus=1 and 
v.estatus=1 and 
vd.estatus=1 and 
p.estatus=1 and 
(a.estatus is null or a.estatus=1) 
and (u.estatus=1 or u.estatus is null) 
group by c.nombrecompleto,c.direccion,c.telefono,c.limitecredito 
,u.nombre 
having sum(vd.preciofinal)>0 

我試過將它轉換爲實體框架,但我不能。 這個我有(我沒有完成)

 from c in Clientes 
join v in Ventas on c.Idcliente equals v.Idcliente 
join vd in Ventadetalles on v.Idventa equals vd.Idventa 
join p in Prestamos on v.Idventa equals p.Idventa 
join a in Abonos on c.Idcliente equals a.Idcliente into al 
from a in al.DefaultIfEmpty() 
join u in Usuarios on a.Creadopor equals u.Idusuario into ul 
from u in ul.DefaultIfEmpty() 
where 
c.Estatus==1 && 
v.Estatus==1 && 
vd.Estatus==1 && 
p.Estatus==1 && 
(a.Estatus==1 || a.Estatus== null) && 
(u.Estatus==1 || u.Estatus== null) 

group new{c.Idcliente,vd,a} by new { c.Nombrecompleto, c.Direccion, c.Telefono, c.Limitecredito, 
usuarionombre=((u.Nombre!=null)?u.Nombre:u.Nombre) 

} 
+0

那麼與實體框架代碼發生那不應該發生? –

+0

不,問題是我不知道如何將sql命令轉換爲實體框架查詢 – angel

+0

如果您使用的是entity-framework,那麼不應該有外鍵讓您進入連接的表嗎? –

回答

1

不知道,但我相信我有

from c in Clientes 
join v in Ventas on c.Idcliente equals v.Idcliente 
join vd in Ventadetalles on v.Idventa equals vd.Idventa 
join p in Prestamos on v.Idventa equals p.Idventa 
join a in Abonos on c.Idcliente equals a.Idcliente into al /*this line*/ 
from a in al.DefaultIfEmpty() /*and this is for to do left outer join or left join*/ 
join u in Usuarios on a.Creadopor equals u.Idusuario into ul 
from u in ul.DefaultIfEmpty() /*so you see here other left join*/ 
where 
c.Estatus==1 && 
v.Estatus==1 && 
vd.Estatus==1 && 
p.Estatus==1 && 
(a.Estatus==1 || a.Estatus== null) && 
(u.Estatus==1 || u.Estatus== null) /*this is for to do (a.estatus is null or a.estatus=1)*/ 

group new 
{ 
vd.Preciofinal,a.Fechacreacion /*these are the max or sum or min columns */} 
by new {c.Idcliente, c.Nombrecompleto, c.Direccion, c.Telefono, c.Limitecredito, 
    usuarionombre=((u.Nombre!=null)?u.Nombre:u.Nombre/*the group by part in a normal sql command */ 
) 
    } 
    into agrupacion /*inside a new table */ 
    where agrupacion.Sum(x=> x.Preciofinal)>0 /*this would be a having sum in a normal sql command */ 
    select new { 
/*my columns i needs only and the sum and max*/ 
agrupacion.Key.Idcliente,agrupacion.Key.Nombrecompleto,agrupacion.Key.Direccion,agrupacion.Key.Telefono, 
    agrupacion.Key.Limitecredito,agrupacion.Key.usuarionombre, saldo=agrupacion.Sum(x=> x.Preciofinal) 
    ,ultimopago=((agrupacion.Max(x=> x.Fechacreacion)!=null)?agrupacion.Max(x=> x.Fechacreacion):DateTime.Now /*ultimopago would be a case when max(fechacreacion)is null then getdate() else max(fechaultimopago)* end as ultimopago/) 
    } 

我希望這可以幫助其他