How to make an if in a select when creating an object?

Asked

Viewed 75 times

1

I don’t know if the title is right, but the question is this::

listaAgendaDetalhe = (from a in listaAgenda
                      join p in listaProfissional
                      on a.idProfissional equals p.id
                      select new agendaDetalhe()
                      {
                      ativo = p.ativo,
                      dataIni = a.dataIni,
                      especialidade = a.especialidade,
                      if (a.frequencia = 0)
                      frequencia = 'DIARIO'
                      else if (a.frequencia = 1)
                      frequencia = 'SEMANAL'
                      else if (a.frequencia = 2)
                      frequencia = 'QUINZENAL'
                      }).ToList();

The doubt is in the field frequencia, I want to do a check before assigning a value to it, but the way I did, it doesn’t work.

2 answers

5

In this case you can use a if ternario for that, would be as follows:

listaAgendaDetalhe = (from a in listaAgenda
                      join p in listaProfissional
                      on a.idProfissional equals p.id
                      select new agendaDetalhe()
                      {
                      ativo = p.ativo,
                      dataIni = a.dataIni,
                      especialidade = a.especialidade,
                      a.frequencia == 0 ? "DIARIO" : a.frequencia == 1 ? "SEMANAL" : a.frequencia == 2 ? "QUINZENAL" : null
                      }).ToList();

2


Use like this:

listaAgendaDetalhe = (from a in listaAgenda
                      join p in listaProfissional
                      on a.idProfissional equals p.id
                      select new agendaDetalhe()
                      {
                          ativo = p.ativo,
                          dataIni = a.dataIni,
                          especialidade = a.especialidade,
                          frequencia = a.frequencia == 0 ? "DIARIO" : a.frequencia == 1 ? "SEMANAL" : "QUINZENAL",
                      }).ToList();

Browser other questions tagged

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