[英]Linq query with left join and group
I'm unable to convert this SQL query into a working linq statement 我无法将此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;
I tryed something like this but i cant handle the select statement 我试过这样的东西,但我无法处理select语句
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 {
I'll appreciate any help! 我会感激任何帮助!
Based on the limited information provided (which tables are certain fields from?), here is what I came up with. 基于提供的有限信息(哪些表是某些字段来自?),这是我想出的。
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)
};
I am not sure which table cena and servis are coming from but to create grouped sum you do something like. 我不确定哪个表格cena和servis来自哪个,但创建分组总和你做的事情。
select new { Sum = grouped.Sum( x => x.cena ) }
and to get max 并获得最大
select new { Max = grouped.Group.Max( x => x.servis ) }
Here is a good reference for you. 这是一个很好的参考。
I've reached this solution (supposing "cena" belongs to MtAuta.FindAll()
): 我已经达到了这个解决方案(假设“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)
};
I've modified your solution little bit and i got it working like this: 我已经修改了你的解决方案,我得到它像这样工作:
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)
};
I just curious if this is the best solution? 我只是好奇这是否是最佳解决方案?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.