Shakti Prakash Singh
Shakti Prakash Singh

Reputation: 2533

LINQ to EF Cross Join issue

I am using Entity Framework 5.

Below is my SQL Query which runs successfully:

Select person.pk 
from person, job
Where 
person.jobId= job.pk 
and 
job.Description = 'CEO'

I have changed the table and column names in the above query.

Now whenI am converting the above query to below LINQ:

from person in Context.Person
from job in Context.Job
where
  person.jobId== job.PK &&
  job.Description == "CEO"
select new {
  person.PK
};

But the above LINQ is giving me an exception:

Unable to create a constant value of type 'Models.Job'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

The LINQ looks simple enough but I am not able to figure out as to what am I missing.

This might be duplicate, but all the questions similar to this one were different and none of them addressed this issue.

Any help would be appreciated.

Upvotes: 1

Views: 463

Answers (1)

Mathew Thompson
Mathew Thompson

Reputation: 56429

That SQL is performing a join, why aren't you performing one in your Linq?

Also, I'm not sure why you're projecting into an anonymous type if you only want one field, just select that field (will map to a List<T> where T is the type of your PK field).

Try this:

from person in Context.Person
join job in Context.Job on person.jobID equals job.PK
where job.Description == "CEO"
select person.PK;

Upvotes: 1

Related Questions