我无法将此SQL查询转换为有效的linq语句
select sum(cena), id_auta, max(servis)
from dt_poruchy left outer join mt_auta on dt_poruchy.id_auta=mt_auta.id
where dt_poruchy.servis>=3 group by id_auta;
我尝试了类似的东西,但我无法处理选择语句
var auta = from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
group ap2 by ap2.ID into grouped
select new {
我会感激任何帮助!
答案 0 :(得分:2)
根据提供的有限信息(哪些表是某些字段来自?),这是我想出的。
var auta = from a in MtAuta.FindAll()
let p = a.DtPoruchys.Where(s => s.SERVIS >= 3)
select new
{
Id = a.Id,
CenaSum = p.Sum(c => c.Cena),
Servis = p.Max(s => s.SERVIS)
};
答案 1 :(得分:1)
我不确定哪个表格cena和servis来自哪个,但创建分组总和你做的事情。
select new { Sum = grouped.Sum( x => x.cena ) }
并获得最大值
select new { Max = grouped.Group.Max( x => x.servis ) }
这是一个很好的参考资料。
答案 2 :(得分:1)
我已达到此解决方案(假设“cena”属于MtAuta.FindAll()
):
var auta = from e in
(from a in DtPoruchy.FindAll()
where a.SERVIS >= 3
join p in MtAuta.FindAll() on a.MtAuta equals p.Id into ap
from ap2 in ap.DefaultIfEmpty()
select new
{
Cena = ap.cena,
IdAuta = a.MtAuta,
Servis = a.servis
})
group e by e.IdAuta into g
select new
{
Cena = g.Sum(e => e.cena),
IdAuta = g.Key,
Servis = g.Max(e => e.servis)
};
答案 3 :(得分:0)
我已经修改了你的解决方案,我得到了这样的工作:
var auta = from jo in
(
from a in MtAuta.FindAll()
join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
from ap2 in ap.DefaultIfEmpty()
where ap2.SERVIS >= 3
select new
{
Cena = ap2.CENA,
Idauto = ap2.ID_AUTA,
Servis = ap2.SERVIS
}
)
group jo by jo.Idauto into g
select new
{
Cena = g.Sum(jo => jo.Cena),
IdAuto = g.Key,
Servis = g.Max(jo => jo.Servis)
};
我只是好奇这是否是最佳解决方案?