2
votes

I am using this query:

     public IEnumerable.....{
        var query = from d in Context.Documentos
        where d.CodigoEquipamento == documentoDTO.CodigoEquipamento &&
        d.Codigo == tipoEquipamentoDTO.Codigo 
        select new DocumentoDTO 
        {
            Codigo = d.Codigo, 
            CodigoEquipamento = d.CodigoEquipamento 
        }
       return query.AsEnumerable < DocumentoDTO>(); 
     }

and i get this Error:

ERROR: The entity or complex type 'Model.DocumentoDTO' cannot be constructed in a LINQ to Entities query.

I am using poco, how i can return a collection of my pocos without all fields?

2

2 Answers

1
votes

You'll have to apply AsEnumerable before the Select for this to work. Otherwise, EF attempts to translate your query into SQL, while the DocumentoDTO is not an Entity - therefore the error.

Context.Documentos.Where(d => d.CodigoEquipamento == documentoDTO.CodigoEquipamento &&
                              d.Codigo == tipoEquipamentoDTO.Codigo).
                   AsEnumerable().
                   Select(d => new DocumentoDTO
                               {
                                   Codigo = d.Codigo,
                                   CodigoEquipamento = d.CodigoEquipamento
                               });
1
votes

You need to return a POCO which isn't a mapped entity. This could be a "real" POCO or an anonymous type. But it cannot be a mapped entity.