繁体   English   中英

将SQL查询转换为linq

[英]Convert Sql query to linq

大家好,我想将此sql查询转换为linq查询。 但我有一些问题,我不知道如何解决,请帮助我。

我的SQL查询

select SERVICESLASBYCOUNTRY.id,site.Name, SERVICESLASBYCOUNTRY.countrycode, sum(SERVICESLASBYCOUNTRYDETAILS.estddays) as estddays
from SERVICESLASBYCOUNTRY
inner join site on SERVICESLASBYCOUNTRY.SiteId = site.id
inner join SERVICESLASBYCOUNTRYDETAILS on SERVICESLASBYCOUNTRY.id = SERVICESLASBYCOUNTRYDETAILS.servicelasbycountrykey
where SERVICESLASBYCOUNTRY.servicecode = 234
group by SERVICESLASBYCOUNTRYDETAILS.servicelasbycountrykey,site.Name, SERVICESLASBYCOUNTRY.countrycode,SERVICESLASBYCOUNTRY.id

这是我尝试的方法,但是有一些错误,请帮助我删除它。

from s in db.SERVICESLASBYCOUNTRies
                               join si in db.sites on s.SiteId equals si.id
                               join sd in db.SERVICESLASBYCOUNTRYDETAILs on s.id equals sd.servicelasbycountrykey 
                               where s.servicecode == servicecode
                               group sd by sd.estddays into bhh 
                               select new SLACountryDTO                        
                               {
                                   ID = s.id,
                                   ServiceCode = s.servicecode,
                                   CountryCode = s.countrycode,
                                   SiteId = s.SiteId,
                                   SiteName = si.Name,
                                   Sum = bhh.Sum(sd => sd.estddays)
                               });

错误是

"s" does not exist current context 
"si" does not exist current context

就像您在SQL查询中完成grouping ,您也需要在LINQ查询中进行以下操作:

from s in db.SERVICESLASBYCOUNTRies
      join si in db.sites on s.SiteId equals si.id
      join sd in db.SERVICESLASBYCOUNTRYDETAILs on s.id equals sd.servicelasbycountrykey 
      where s.servicecode == servicecode
      group sd by new { sd.estddays, s.countrycode,s.servicecode,s.id,s.SiteId,si.Name } 
                  into bhh 
      select new SLACountryDTO                        
      {
           ID = bhh.Key.id,
           ServiceCode = bhh.Key.servicecode,
           CountryCode = bhh.Key.countrycode,
           SiteId = bhh.Key.SiteId,
           SiteName = bhh.Key.Name,
           Sum = bhh.Sum(sd => sd.estddays)
       });

您的bhh小组有一个键和一个项目集合,您应该检查该对象以获取数据。 s&si在您所在的组中无效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM