使用let时Linq错误

时间:2014-08-15 16:40:49

标签: linq let

我有以下linq查询,它给出以下错误:查询正文必须以select子句或group子句结束

from sdc in ScheduleDestinationCaches
let twins = from gt in GetTwins(72)
join ssFilter in ScheduleStops on sdc.CarrierID equals ssFilter.CarrierID
join ssLookup in ScheduleStops on sdc.CityID equals ssLookup.CityID
join sc in ScheduleCarriers on ssLookup.CarrierID equals sc.CarrierID
where twins.Contains(ssFilter.StopID ) || ssFilter.StopID == 72
select sc

任何想法?

顺便说一句,我使用linqpad

1 个答案:

答案 0 :(得分:0)

让我们添加一些缩进:

from sdc in ScheduleDestinationCaches
let twins = from gt in GetTwins(72)
            join ssFilter in ScheduleStops on sdc.CarrierID equals ssFilter.CarrierID
            join ssLookup in ScheduleStops on sdc.CityID equals ssLookup.CityID
            join sc in ScheduleCarriers on ssLookup.CarrierID equals sc.CarrierID
            where twins.Contains(ssFilter.StopID ) || ssFilter.StopID == 72
            select sc

你现在能看到这个问题吗?将from作为赋值添加到let - 已定义的变量将启动新的LINQ表达式,该表达式以select sc结尾。这使得外部查询根本没有select子句!

我认为你正在寻找

from sdc in ScheduleDestinationCaches
let twins = GetTwins(72)
join ssFilter in ScheduleStops on sdc.CarrierID equals ssFilter.CarrierID
join ssLookup in ScheduleStops on sdc.CityID equals ssLookup.CityID
join sc in ScheduleCarriers on ssLookup.CarrierID equals sc.CarrierID
where twins.Contains(ssFilter.StopID ) || ssFilter.StopID == 72
select sc

基于评论的更新

from sdc in ScheduleDestinationCaches
let twins = GetTwins(72).Select(x => x.Id)
join ssFilter in ScheduleStops on sdc.CarrierID equals ssFilter.CarrierID
join ssLookup in ScheduleStops on sdc.CityID equals ssLookup.CityID
join sc in ScheduleCarriers on ssLookup.CarrierID equals sc.CarrierID
where twins.Contains(ssFilter.StopID ) || ssFilter.StopID == 72
select sc