Lambda or Linq brings me all the fields and not only those of expression

Asked

Viewed 236 times

0

Just for learning even, understand the section. I did a lambda of a bank with only 6 fields, like this:

 public List<LiberacaoDTO> getAutoriza(int idorcamento)
    {
        var lista = contexto.Liberacoes
       .Where(m => m.IdOrcamento == idorcamento)
       .Select(m => new LiberacaoDTO
       {
           TipoVenda = m.TipoVenda,
           Juros = m.Juros != 0 ? m.Juros : 0,
           Entrada = m.Entrada != 0 ? m.Entrada : 0,
           MaxComi = m.MaxComi,
           Cliente = m.Cliente,
           Filial = m.Filial
       })
       .ToList();

        return lista;
    }

When the service returns, it brings all the fields. Only those listed in the expression, with value, the others have null or 0. But why all fields and not only those listed in the expression? Below the call of my service:

public class LiberacaoController : ApiController
    {
        AutorizadorContext contexto = new AutorizadorContext();
        PedidoLiberacao liberacao = new PedidoLiberacao();

        [AcceptVerbs("Get")]
        public IEnumerable<LiberacaoDTO> getLiberacao()
        {
            return liberacao.getAutoriza(1000012093).AsEnumerable().ToList();
         }
    }

1 answer

4


Fields you did not specify in . Select are initialized with their default value. For example, all fields int will have the value 0, all fields string null and all fields DateTime will have the value 1/1/0001 12:00:00 AM.

This is because . Select creates a new instance of the object LiberacaoDTO.

If you want it to return only the . Select fields you can use an anonymous object in . Select:

.Select(m => new 
 {
     TipoVenda = m.TipoVenda,
     Juros = m.Juros != 0 ? m.Juros : 0,
     Entrada = m.Entrada != 0 ? m.Entrada : 0,
     MaxComi = m.MaxComi,
     Cliente = m.Cliente,
     Filial = m.Filial
 })

The problem with this approach is that you will miss some of the typing.

  • I understood Thiago, I’ve done so and had difficulty in the return, but I think it is because I expected a Liberacaodto and so gave dick and left aside, because I solved as indicated in the post. I will try this approach by returning a object, and see if caught. Because this will be consumed in an Android App (Xamarin) and the less irrelevant data, the better, I think.

  • If you don’t need this data maybe it’s a better idea to clean up your DTO, or create another DTO that has only what it takes. The DTO does not need to be a reflection of the bank in all cases.

  • A DTO can have fields of two or more tables, to be used only in an expression that requires these fields?

  • Yes, but the best way to do it is to create two Dtos and one to have a property that is the other.

  • Like getting a Icollection<Other> from another? That’s it?

  • That’s right. See my answer in your other question.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.