views:

92

answers:

1

Hey guys

Just wondering how the following sql query would look in linq for Entity Framework...

SELECT  KPI.*
FROM    KeyPerformanceIndicator KPI
    INNER JOIN (
        SELECT  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

+1  A: 

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 ...
Craig Stuntz
I am trying to bring across the query you provided but the 'Any' method isn't a part of SportProgramPlans... any ideas?
vdh_ant
Is `KeyPerformanceIndicator.SportProgramPlans` 1:1?
Craig Stuntz
KeyPerformanceIndicator has only 1 SportProgramPlan but 1 SportProgramPlan can have many KeyPerformanceIndicators...
vdh_ant
OK; updated for that cardinality, but see the note; I'm not sure your original SQL is right.
Craig Stuntz