asp.net-mvc – Could not create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context

Question:

I'm trying to make a query in LINQ, and it's returning the following error message:

Could not create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context

A structure is recursive, so one structure can be a child of another.

When I get the idPai , I want it to bring me only the children of that structure, and when I pass the id , I want that specific structure, from that idPai .

The Action would look like this:

Estrutura/{idPai}/{id}

The query is as follows:

var list = (from e in ent.Estrutura
join t in ent.TipoEstrutura on e.idTipoEstrutura equals t.id

join ee in ent.Estrutura on e.idEstrutura equals ee.id into eleft
from ePai in eleft.DefaultIfEmpty()


where ((idPai.HasValue ? e.idEstrutura.Equals(id) : e.idEstrutura.Equals(null))
        && (id.HasValue ? e.id.Equals(id) : e.id != null))

select new SigProcessos.Entity.ViewModel.Estrutura
{
    Id = e.id,
    IdEstrutura = e.idEstrutura,
    IdTipoEstrutura = t.id,
    DescricaoEstrutura = e.descricao,
    DescricaoEstruturaPai = (null != ePai ? ePai.descricao : default(string))
}).ToList();

return list;

Thanks for the help!

Answer:

Try replacing your .Equals() with "==".

How do you have variables that are likely to be of type int? , it might be a problem because the program might be trying to do .Equals() with incorrect types (example: int?.Equals(int) ).

Scroll to Top