复杂linq查询中的Where子句给出以下错误“;查询正文必须以select子句或group子句linq”结尾;
本文关键字:子句 linq 查询 正文 select group 结尾 Where 错误 复杂 | 更新日期: 2023-09-27 18:24:43
我有一个棘手的linq查询,当我在它的一个子查询中放入"where"子句时,它一直会给我带来麻烦。
这是我正在使用的模式:
汽车
名称-(nvarchar)
车库_Id-(唯一标识符)-FK
Id-(唯一标识符)-PK
车库
Id(唯一标识符)-PK
名称(nvarchar)
CarTire记录-(日期时间)-PK
CarId-(唯一标识符)-PK
TireId-(uniqueIdentifier)-PK
轮胎
序列号-(varchar)Id-(唯一标识符)-PK
侧壁冲击
记录-(日期时间)-PK
TireId-(唯一标识符)-PK,FK
TreadImpact
记录-(日期时间)-PK
TireId-(唯一标识符)-PL、FK
车身碰撞
记录-(日期时间)-PK
CarId-(唯一标识符)-PK,FK
这就是我试图用我的查询实现的(请原谅糟糕的格式):
名称---------------Id------------侧面碰撞--踏板碰撞-----车身碰撞色轿车--------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();
}
林克垫扼流圈在以下线路上:
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子句移到各处,但仍然会出现同样的错误。如有任何提示,我们将不胜感激。请注意,我没有将join包含在BodyImpact表中,以保持相对简短。谢谢
布鲁诺,
在from子句和group-by-clause 之间移动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。已录制到。也已录制。
我希望这能帮助