Converting sql query to EF query- nested query in from
Just wondering how the following sql query would look in linq for Entity Framework...
SELECT KPI.*
FROM KeyPerformanceIndicator KPI
INNER JOIN (
SE开发者_如何学运维LECT SPP.SportProgramPlanId
FROM SportProgramPlan PSPP
INNER JOIN SportProgramPlan ASPP
ON (PSPP.SportProgramPlanId = @SportProgramPlanId
AND PSPP.StartDate >= ASPP.StartDate
AND PSPP.EndDate <= ASPP.EndDate)
) AS SPP
ON KPI.SportProgramPlanId = SPP.SportProgramPlanId
Cheers Anthony
Hard to say without seeing the associations in your model. Would there be a self-referential association on SportProgramPlan
?
The SQL seems like an error to me as PSPP
and ASPP
could be the same record, and I'm not sure you want that? At any rate, it's trivial to exclude....
Here's a shot at it:
var q = from kpi in Context.KeyPerformanceIndicators
where kpi.SportProgramPlan.Id = sportProgramPlanId
&& Context.SportProgramPlans.Any(aspp =>
spp.StartDate >= aspp.StartDate
&& spp.EndDate <= aspp.EndDate))
select ...
精彩评论