Skip to main content
 首页 » 编程设计

c#之具有左连接和组的 Linq 查询

2024年02月24日10Free-Thinker

我无法将此 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<br/> where dt_poruchy.servis>=3 group by id_auta;

我试过类似的方法,但我无法处理 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 { 

我将不胜感激!

请您参考如下方法:

根据提供的有限信息(哪些表是某些字段?),这是我想出的。

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) 
           };