Using the Case When Then command with Entity Framework

Asked

Viewed 554 times

2

I have the following structure corresponding to a table in my database:

Cat_Codigo, 
Cat_Descricao, 
Cat_Observacao, 
Cat_Status

Where the Cat_Status is defined by 0 or 1, with 0 -> Deactivative and 1 -> Active.

However I do not want to display to the final user, 0 or 1, but rather Target and Active, I know that this is possible to be done through the following SQL command

SELECT  cat_codigo As Codigo,  cat_descricao As Descricao, cat_observacao AS Observacao, 
CASE cat_status WHEN '1' THEN 'Ativo' WHEN '0' THEN 'Desativo' END AS Status FROM Categoria

Using the command CASE I can define that.

Now how do I do the same thing through the Entity Framework? What method can I use to change fields to a name. In my class the status attribute is as int, That would cause some trouble too?

3 answers

2


A simple way is to create an attribute with only get. It will not be stored in the database, and should suit you. In addition, it is reusable.

public class Categoria 
{
    public int Cat_Codigo  { get; set; }
    public string Cat_Descricao { get; set; }
    public string Cat_Observacao { get; set; }
    public int Cat_Status { get; set; }

    // c# 6
    public string DescricaoStatus => Cat_Status == 1 ? "Ativo" : "Inativo";
}

or

public string DescricaoStatus 
{
  get 
  {
    return Cat_Status == 1 ? "Ativo" : "Inativo";
  }
}
  • Thanks, I managed to solve my problem with your information. well, I have another question related to Entity framework, can I ask you a question here or should I create a new topic?

  • Give a search on existing questions. If you do not find the answer you can ask a new question (topic). The correct term here is question as it is not forum.

1

One possibility would be to use the conditional operator ?: returns one of two values, depending on the value of a boolean expression.

var categorias = context.Categoria.
        Select(a => new {
            Codigo = a.Cat_Codigo,
            Descricao = a.Cat_Descricao,
            Observacao = a.Cat_Observacao,
            Status = a.Cat_status == 0 ? "Ativo" : "Desativo"
        });
  • In the case when I do Status = a.Cat_status == 0 ? "Active" : "Disable" and my Status attribute is an integer no problem? in case I wouldn’t assign the Active or Deactivative value to the Status variable?

  • In this example I showed you the Status will be a string, because I am returning an anonymous type.

0

I recommend that you create an object of the Daoretorno type with equal fields of the Database only with a string attribute to be able to assign the value after listing, in the case a statusstr (string str).

Create a list like Daoretorno.

List<DaoRetorno> lista = conexao.Categoria.Select(x=> new DaoRetorno () { codigo = x.codigo, descricao = x.descricao, obs= x.obs, status=x.status }).toList();

foreach (item i in lista)
{
  if (i.status = 1){
       i.statusstr = 'Ativo';
  }
  else
  {
       i.statusstr = 'Desativo';
  }
}

That is to pass one by one and assign the value in the list, after that your list will have the field filled, only use the value after.

Browser other questions tagged

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