Felipe Deguchi
Felipe Deguchi

Reputation: 586

Unable to create a constant value of type '?'. Only primitive types or enumeration types are supported in this context

On the following code I am getting an exception

var ret = db.Especialidades.Except(sol.EspecialidadesExigidas).ToList();

This is the Exception

Unable to create a constant value of type 'TCC.Models.Especialidade'. 
Only primitive types or enumeration types are supported in this context.

I have looked into other similar questions and tried to adapt their answers, but to no success. Other atempts made:

var ret = (from e in db.Especialidades where !sol.EspecialidadesExigidas.Any(e2 => e2.Id == e.Id) select e).ToList(); 
var ret = (from e in db.Especialidades where !sol.EspecialidadesExigidas.Select(e2 => e2.Id).Contains(e.Id) select e).ToList();

What I am trying to do is to get all "Especialidades" from the database that`s not included in the list

Upvotes: 2

Views: 3372

Answers (1)

juharr
juharr

Reputation: 32296

What about using a list of ids instead so the Linq provider will know how to translate it into SQL.

var ids = sol.EspecialidadesExigidas.Select(e => e.Id).ToList();
var ret = db.Especialidades.Where(e => !ids.Contains(e.Id));

Upvotes: 5

Related Questions