将SQL查询转换为Linq(需要帮助才能将SQL查询写入Linq)

将SQL查询转换为Linq(需要帮助才能将SQL查询写入Linq),sql,linq,Sql,Linq,这是我的查询返回我想要的准确结果。我想用LINQ写这个 select i.reportdate,co.naam,i.issueid,i.vrijetekst,i.lockuser,te.teamnaam, count(ie.issueid) as events, sum(ie.bestedetijd) as Tijd from company co,hoofdcontracten hc,subcontracten sc,sonderhoud so,g2issues i

这是我的查询返回我想要的准确结果。我想用LINQ写这个

select i.reportdate,co.naam,i.issueid,i.vrijetekst,i.lockuser,te.teamnaam, count(ie.issueid) as events, sum(ie.bestedetijd) as Tijd  
            from company co,hoofdcontracten hc,subcontracten sc,sonderhoud so,g2issues i,g2issueevents ie, g2issuestatus iss,teams te,locatie l 
            Where co.companyid = hc.companyid And 
            hc.hcontractid = sc.hcontractid and 
            so.scontractid = sc.scontractid and 
            sc.scontractid = i.scontractid and 
            i.issueid = ie.issueid and 
            so.teamid = te.teamid and 
            ie.locatieid = l.locatieid and 
            l.bezoek = 0 and 
            i.issuestatusid = iss.issuestatusid and 
            fase < 7 and 
           co.companyid <> 165 
           group by i.reportdate,co.naam,i.issueid,i.vrijetekst,i.lockuser,te.teamnaam ,i.reportdate
           having sum(ie.bestedetijd)>123
选择i.reportdate、co.naam、i.issueid、i.vrijetekst、i.lockuser、te.teamnaam、count(ie.issueid)作为事件、sum(ie.bestedetijd)作为Tijd
公司公司、分包商hc、分包商sc、sonderhoud so、G2问题i、G2问题ie、G2问题iss、团队te、地点l
其中co.companyid=hc.companyid和
hc.hcontracid=sc.hcontracid和
so.scontracid=sc.scontracid和
sc.scontracid=i.scontracid和
i、 issueid=即issueid和
so.teamid=te.teamid和
ie.LOCATEID=l.LOCATEID和
l、 bezoek=0和
i、 issuestatusid=iss.issuestatusid和
fase<7和
公司ID 165
按i.reportdate、co.naam、i.issueid、i.vrijetekst、i.lockuser、te.teamnaam、i.reportdate分组
总和大于123的
我正在尝试这一点,但对select子句感到困惑。如何在select子句和group by子句中使用聚合函数

var myList = (from co in _context.Company
                          from hc in _context.Hoofdcontracten
                          from sc in _context.Subcontracten
                          from so in _context.Sonderhoud
                          from i in _context.G2issues
                          from ie in _context.G2issueEvents
                          from iss in _context.G2issueStatus
                          from te in _context.Teams
                          from l in _context.Locatie
                          where
                          co.CompanyId == hc.CompanyId
                          && hc.HcontractId == sc.HcontractId
                          && so.ScontractId == sc.ScontractId
                          && sc.ScontractId == i.ScontractId
                          && i.IssueId == ie.IssueId
                          && so.Teamid == te.Teamid
                          && ie.LocatieId == l.LocatieId
                          && l.Bezoek == false
                          && i.IssuestatusId == iss.IssueStatusId
                          && iss.Fase < 7
                          && co.CompanyId != 165
                          select new { }).ToList();
var myList=(来自_context.Company中的co
来自hc in_context.Hoofdcontracten
来自分包商中的sc
从so in_context.Sonderhoud
来自i in_context.g2问题
来自ie in_context.g2事件
来自iss in_context.G2issueStatus
来自te in_context.Teams
来自l in_context.Locatie
哪里
co.CompanyId==hc.CompanyId
&&hc.hcontracid==sc.hcontracid
&&so.scontracid==sc.scontracid
&&sc.scontracid==i.scontracid
&&i.IssueId==ie.IssueId
&&so.Teamid==te.Teamid
&&ie.LOCATEID==l.LOCATEID
&&l.Bezoek==错误
&&i.IssuestatusId==iss.IssuestatusId
&&iss.Fase<7
&&公司ID!=165
选择new{}.ToList();

我的!有人试图节省几分钟的打字时间,使用各种不可识别的变量名,如hc、sc、i、ie、iss,而不用担心这会使理解所发生的事情所需的时间增加十倍

我还没有完全解释您的查询,显然您认为不需要显示实体框架类以及它们之间的关系

我收集到的信息是,您希望执行一个大连接,然后从连接中选择几列。您希望将结果集合分组为具有相同reportdate、name、issueid等的项目组。。。。您需要每个组中所有项目的bestede tijd,以及组中项目的数量

LINQ有两种类型的语法,它们的作用实际上是相同的:。虽然我可以阅读查询语法,但我更熟悉方法语法,因此我的答案将使用方法语法。我想你会明白发生了什么

我将以较小的步骤完成,如果您愿意,可以将所有步骤合并为一个步骤

首先进行大连接,然后选择一些列。使用查询语法编写时,大连接是少数几个更容易的语句之一(请参阅)

(顺便说一下:你在GroupBy中两次提到reportdate,我想这不是你的意思)

此分组是使用

结果是组的集合。每个组都包含具有相同ReportDate、CompanyName等值的原始bigJoin项。此公共值位于group.Key中

现在,从每个组中,您需要以下项目:

  • 组中的几个常用值(ReportDate、CompanyName、IssueId等)。我们从小组的钥匙上拿到的
  • Tijd=组中所有元素的ie.bestedeTijd之和
  • Events=组中所有元素的ie.IssueId数。由于组中的每个元素只有一个ie.IssueId,因此结果也是组中元素的数量
根据该结果,您只希望保留Tijd>123的值

因此,我们将在组上进行新的选择,然后在Tijd上进行Where

var result = groups.Select(group => new
 {
     Tijd = group.Sum(groupElement => groupElement.ie.bestedetijd),
     Events = group.Count(),

     // the other fields can be taken from the key
     ReportDate = group.Key.reportdate,
     CompanyName = group.Key.CompanyName,
     IssueId = group.Key.Issueid,
     FreeText = group.Key.FreeText,
     LockUser = group.Key.LockUser,
     TeamName = group.Key.TeamName, 
})
.Where(resultItem => resultItem.Tijd > 123);
{
    i.reportdate,
    co.naam,
    i.issueid,
    i.vrijetekst,
    i.lockuser,
    te.teamnaam,
}
var groups = bigjoin.GroupBy(joinedItem => new
    {   // the items to group on: all elements in the group have the same values
        ReportDate = i.Reportdate,
        CompanyName = co.naam,
        IssueId = i.issueid,
        FreeText = i.vrijetekst,
        LockUser = i.lockuser,
        TeamName = te.teamnaam,
    });
var result = groups.Select(group => new
 {
     Tijd = group.Sum(groupElement => groupElement.ie.bestedetijd),
     Events = group.Count(),

     // the other fields can be taken from the key
     ReportDate = group.Key.reportdate,
     CompanyName = group.Key.CompanyName,
     IssueId = group.Key.Issueid,
     FreeText = group.Key.FreeText,
     LockUser = group.Key.LockUser,
     TeamName = group.Key.TeamName, 
})
.Where(resultItem => resultItem.Tijd > 123);