2

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

Felipe Deguchi
  • 586
  • 1
  • 7
  • 25
  • Possible duplicate of [LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context](http://stackoverflow.com/questions/13405568/linq-unable-to-create-a-constant-value-of-type-xxx-only-primitive-types-or-enu) – Daniel Gimenez Nov 17 '15 at 15:30
  • Is EspecialidadesExigidas a DbSet? – Nickthename Nov 17 '15 at 15:32
  • No, its an ICollection – Felipe Deguchi Nov 17 '15 at 15:48
  • @DanielGimenez, As I said, i looked into other pages, I even tried the solution given by them, as I posted on the question, but that also got me a "you cant do that" from Visual Studio – Felipe Deguchi Nov 17 '15 at 15:50

1 Answers1

5

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));
juharr
  • 31,741
  • 4
  • 58
  • 93
  • Thanks a lot, it works. But, it fells kinda dirty, having to do this. There should be a way to do it without having to take that extra step of getting the Id`s – Felipe Deguchi Nov 17 '15 at 15:32