views:

17

answers:

2

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?

+1  A: 

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.

Craig Stuntz
+1  A: 

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
                               });
Yakimych