0
votes

I am getting the following error:

"An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: Unable to create a constant value of type 'Datos.Especialidad'. Only primitive types or enumeration types are supported in this context."

When trying to correct the following code:

IQueryable<Medico> listaMedicos = 
    from unMedico in context.Medico
    select unMedico;

if (medico.Especialidad.Count > 0)
{
    listaMedicos = FiltrarPor(listaMedicos, 
        x => x.Especialidad.Any(e => e.ID == (medico.Especialidad.FirstOrDefault().ID)));
}

return listaMedicos.ToList();

The relationship between "Medico" and "Especialidad" is a many to many as follow

enter image description here

1
What exactly is FiltrarPor doing? You probably just need to capture medico.Especialidad.FirstOrDefault().ID in a variable and use that in your lambda instead.juharr
this is "FiltrarPor" signature IQueryable<TEntity> query,Expression<Func<TEntity, bool>> filter = null) and executes: query = query.Where(filter);alvardo
You should add that code to the question.juharr
@RufusL That's being used in a Where so they do want a Any.juharr
Try doing var id = medico.Especialidad.FirstOrDefault().ID; and then listaMedicos = FiltrarPor(listaMedicos, x => x.Especialidad.Any(e => e.ID == id));juharr

1 Answers

0
votes

Try:

if (medico.Especialidad.Count > 0)
{
    var medicoId = medico.Especialidad.FirstOrDefault().ID
    listaMedicos = FiltrarPor(listaMedicos, 
        x => x.Especialidad.Any(e => e.ID == medicoId));
}

actually, better, would be:

var medicoEspecialidad = medico.Especialidad.FirstOrDefault();
if (medicoEspecialidad != null)
{
    var medicoId = medicoEspecialidad.ID
    listaMedicos = FiltrarPor(listaMedicos, x => x.Especialidad.Any(e => e.ID == medicoId));
}