Mike Christensen
Mike Christensen

Reputation: 91618

Is there some sort of syntax error with this LINQ JOIN?

I've looked at various questions on SO and other sites, and this appears to be the correct syntax to perform a JOIN in LINQ, however it's just not working:

var stages = (from stage in entityManager.TPM_TASKSTAGE select stage);
var results = (from task in pv.TPM_TASK
               join st in stages on st.STAGEID equals task.STAGEID
               where task.TASKTYPE == "Solution"
               select new SolutionTask());

Ignore, for now, the fact that I don't actually select anything of interest, but I'd like to have access to the st.NAME property on each row of TPM_TASK. The two tables are linked by STAGEID. I get the compiler error:

The name 'st' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.

In the LINQ join expression, both st and task have red squigglies. Please tell me I'm doing something dumb.

Upvotes: 14

Views: 9456

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

Key selector from outer sequence should go first. Outer sequence in your case is pv.TPM_TASK. So, you should join on task.STAGEID equals st.STAGEID

var stages = (from stage in entityManager.TPM_TASKSTAGE select stage);
var results = (from task in pv.TPM_TASK
               join st in stages on task.STAGEID equals st.STAGEID // here
               where task.TASKTYPE == "Solution"
               select new SolutionTask());

Upvotes: 19

Related Questions