复杂linq查询中的Where子句给出以下错误“查询主体必须以select子句或group子句linq结尾”

时间:2014-09-26 14:57:43

标签: c# sql linq linqpad

我有一个棘手的linq查询,当我在其中一个子查询中放入“where”子句时,它会不断给出麻烦。

这是我正在使用的架构:

汽车
名称 - (nvarchar)
Garage_Id - (uniqueidentifier) - FK
Id - (uniqueIdentifier) - PK

车库
Id(Uniqueidentifier) - PK
名称(nvarchar)

卡地亚 录音 - (日期时间) - PK
CarId - (uniqueidentifier) - PK
TireId - (uniqueIdentifier) - PK

轮胎
SerialNumber - (varchar) Id - (uniqueidentifier) - PK

SidewallImpact
录音 - (日期时间) - PK
TireId - (uniqueidentifier) - PK,FK

TreadImpact
录音 - (日期时间) - PK
TireId - (uniqueidentifier) - PL,FK

CarBodyImpact
录音 - (日期时间) - PK
CarId - (uniqueidentifier) - PK,FK

这就是我想用我的查询实现的目标(原谅糟糕的格式化):

  

名称 ----------- ID ---------- lidimpact - treadimpact ----- carbodyimpact
  红车-------- 12334 ------ 1 ---------------------- 22 --------- ----------- 34

我可以使用以下sql查询得到我想要的内容:

SELECT TOP(12) cr.NAME, 
           cr.id, 
           t.sidewallimpact, 
           p.treadimpact, 
           s.carbodyimpact, 
           ( t.sidewallimpact
             + p.treadimpact
             + s.carbodyimpact ) Totals 
FROM   car cr 
   JOIN (SELECT th.carId, 
                Count(*) sidewallimpact
         FROM   CarTire th 
                JOIN tires ti 
                  ON th.tireid = ti.id 
                JOIN sidewallimpact tah 
                  ON ti.id = tah.tireid 
         WHERE  ( tah.recorded >= '01/01/2014' 
                  AND tah.recorded <= '09/25/2014' ) 
         GROUP  BY th.carId) t 
     ON cr.id = t.carid 
   JOIN (SELECT th.carid, 
                Count(*) treadimpact
         FROM   CarTire th 
                JOIN tires ti 
                  ON th.tireid = ti.id 
                JOIN treadimpact pah 
                  ON ti.id = pah.tireid 
         WHERE  ( pah.recorded >= '01/01/2014' 
                  AND pah.recorded <= '09/25/2014' ) 
         GROUP  BY th.carid) p 
     ON cr.id = p.carid 
   JOIN (SELECT cr.id, 
                Count(*) carbodyimpact 
         FROM   car cr 
                JOIN carbodyimpact sah 
                  ON cr.id = sah.carid 
         WHERE  ( sah.recorded >= '01/01/2014' 
                  AND sah.recorded <= '09/25/2014' ) 
         GROUP  BY tr.id) s 
     ON cr.id = s.id 
WHERE  cr.garage_id = 'A6087B27-6E18-4B50-B8A6-7E1F746A312E' 
ORDER  BY totals DESC 

但是当我尝试使用linqpad将其翻译为linq时,我遇到了问题。这就是我现在在linqpad中所拥有的:

Void Main()
{
var top12 = from tr in Cars
where tr.Garage_Id == new Guid("A6087B27-6E18-4B50-B8A6-7E1F746A312E")
    join t in (
        from th in CarTire
        join ti in Tires
        on th.TireId equals ti.Id 
        join tah in SideWallImpact                      
        on ti.Id equals tah.TireId              
        where (th.Recorded >= new DateTime(2014, 01,01) && th.Recorded <= new DateTime(2014,9,25))

        into j1     
        from j2 in j1
        group j2 by th.CarId into grouped       
        select new { CarId = grouped.Key, Count = grouped.Count(t => t != null)}
    ) on tr.Id equals t.CarId
    join p in
    (
    from th in CarTire
        join ti in Tires
        on th.TireId equals ti.Id 
        join pah in TreadImpact                     
        on ti.Id equals pah.TireId              
        where (th.Recorded >= new DateTime(2014, 01,01) && th.Recorded <= new DateTime(2014,9,25))

        into j1     
        from j2 in j1
        group j2 by th.CarId into grouped       
        select new { CarId = grouped.Key, Count = grouped.Count(t => t != null)}
    )
     on tr.Id equals p.CarId
    select new {Name = tr.Name, Id = tr.Id,  SidWallCount = t.Count, TreadCount = p.Count };

    top12.Dump();
}

Linq pad窒息在以下一行:

where (tah.Recorded >= new DateTime(2014, 01,01) && tah.Recorded <= new DateTime(2014,9,25))

并提供此错误:

"A query body must end with a select clause or a group clause linq"

我已尝试将 where 子句移到任何地方,但仍然会遇到相同的错误。任何提示将不胜感激。请注意,我没有将连接包含在BodyImpact表中以保持相对较短的时间。谢谢!

1 个答案:

答案 0 :(得分:1)

布鲁诺,

移动from子句和group by子句之间的where子句

来自这个

where (tah.Recorded >= new DateTime(2014, 01,01) && tah.Recorded <= new DateTime(2014,9,25))
into j1     
from j2 in j1
group j2 by th.CarId into grouped

到这个

into j1     
from j2 in j1
where (th.Recorded >= new DateTime(2014, 01,01) && th.Recorded <= new DateTime(2014,9,25))
group j2 by th.CarId into grouped 

我认为你需要将tah.Recorded重命名为th.Recorded。

我希望这会有所帮助