How to check and convert Ienumerable values from a database query?

Asked

Viewed 54 times

0

I have one that returns a table in Ienumerable and I have a certain column that returns values "A" for Active, "C" for Canceled and so on. I would like to convert these values to the correct one before arriving in the view. Type instead of "A" would appear to the "Active" user. How do you do that? Any help is welcome.

It would be something like the code below, more applied in a Ienumerable list.

public string GetStatusPagamento(int fileCodigo)
 {
  var statusPgto = _context.GetFileByFileCode(fileCodigo).Select(s => 
   s.StatusPagamento).FirstOrDefault();

    switch (statusPgto.ToString())
    {
    case "A":
      return "Ativo";

    case "D":
      return "Ativo";

    case "E":
      return "Cancelado";

    case "M":
      return "Reembolsado";

    case "R":
      return "Ativo";

    case "X":
      return "Cancelado";

    default:
      return "Indefinido";
  } 
}
  • It is very difficult to answer because anyone who reads your question can imagine your code differently. You need to ask the questions in detail, have a review because there are some words missing from the question, add your code and learn more about how to ask here: https://answall.com/help/how-to-ask

  • I added an example code. I needed to do something like this, but in a list like Ienumerable.

  • @Nilsonmartins You want to do this "conversion" for all items from statusPgto. That’s it?

  • What is the type of statusPgto?

  • This conversion would be for all items. It is a string list "Ienumerable<string>"

  • Okay, it was in the text of the question, I didn’t notice. What led me to question was the statusPgto.ToString(), used in the code.

  • He considered the idea of overriding the . Tostring() of the [Statuspagamento] to make this conversion?

  • 2

    @Ronaldoaraújoalves and if in another context you need another way? In fact every time someone uses this method to format data a panda dies somewhere https://answall.com/q/212754/101

  • Well noted, @Maniero...you’re right. + 1

  • Thanks @Ronaldoaraújoalves, override Tostring was the solution I was looking for.

  • Thanks @Maniero, override Tostring was the solution I was looking for.

Show 6 more comments

1 answer

1


The solution I found was to create another attribute called "Switchstatuspagto" in my model that makes a state switch case and I call this attribute directly in my View.

public string SwitchStatusPagto
    {
      get
      {
        if (!string.IsNullOrEmpty(StatusPagto))
        {
          switch (StatusPagto)
          {
            case "A":
            case "D":
            case "R":
              return "Ativo";
            case "E":
            case "X":
              return "Cancelado";
            case "M":
              return "Reembolsado";
            default:
              return StatusPagto;
          }
        }
        return string.Empty;
      }
    }

<td>@Html.DisplayFor(fr => itemRequisicao.SwitchStatusPagto)</td>

Note: "Return string. Empty" was required to avoid Null Reference error if the data is null.

Browser other questions tagged

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