Compare string with enum, within lambda?

1

The problem is:

I have a dropdownlist loaded from an enum, which may come with the null value of the screen.

listaItensSolicitados = os.Site == null ? 
   this.itemSolicitadoService.Consultar(o =>
            o.ItemServico.Servico.Chave == 9 &&
            o.ChaveOrgao == os.Orgao.Chave && o.Numero.Contains(ramal)
            && o.CategoriaDiurna == ddlCategoriaDiurna
            && o.Instalado == true
            && (o.Depreciado == null
            || o.Depreciado == false)).Take(200).ToList() :

As you know, you can not use ToString() within lambda , I would like to know if there is a way for me to make this comparison, in my ActionResult I get the value of DDL as string , and when the value null comes, it does not work.

@edit

This is my enum :

public enum CategorizacaoRamaisEnum : int
{     
    [Description("Categoria 1")]
    Categoria1 = 1,
    [Description("Categoria 2")]
    Categoria2,
    [Description("Categoria 3")]
    Categoria3,
    [Description("Categoria 4")]
    Categoria4,
    [Description("Categoria 5")]
    Categoria5,
    [Description("Categoria 6")]
    Categoria6,
    [Description("Categoria 7")]
    Categoria7,
    [Description("Categoria 8")]
    Categoria8,
    [Description("Categoria 9")]
    Categoria9,
    [Description("Categoria 10")]
    Categoria10,
    [Description("Categoria 11")]
    Categoria11       
}

@edit 2

Creating dropdownlist :

@Html.DropDownList("ddlCategoriaDiurna", 
           (ViewBag.CategoriaDiurna as SelectList), 
            string.Empty)                   
    
asked by anonymous 09.12.2016 / 15:53

3 answers

1

I was able to resolve it as follows:

I created a method to fetch the string passed on screen in my enum, if it was null value, return to category1 by default:

public CategorizacaoRamaisEnum retornarEnum(string ddlSelecionado) 
{
     return string.IsNullOrEmpty(ddlSelecionado) 
             ? CategorizacaoRamaisEnum.Categoria1 
             :(CategorizacaoRamaisEnum)System.Enum.Parse(typeof(CategorizacaoRamaisEnum),
               ddlSelecionado);
}

I validated the return and if the value passed existed in enum :

CategorizacaoRamaisEnum retornoCategoriaDiurna = retornarEnum(ddlCategoriaDiurna);
bool result = Enum.GetNames(typeof(CategorizacaoRamaisEnum)).Contains(ddlCategoriaDiurna);

And finally in my query Linq :

listaItensSolicitados = os.Site == null ?      this.itemSolicitadoService.Consultar(o =>
            o.ItemServico.Servico.Chave == 9 &&
            o.ChaveOrgao == os.Orgao.Chave
            && o.Numero.Contains(ramal)
            && o.Instalado == true          
            && (result ? o.CategoriaDiurna == retornoCategoriaDiurna : o.Instalado == true)
            && (o.Depreciado == null
            || o.Depreciado == false)).Take(200).ToList()

I know it was redundant, in the fake of the ternary, but that way solved my problem. If someone thinks of a more elegant way, kindly put there, that will help me to improve.

    
09.12.2016 / 18:35
2

Does the value of this ddlCategoryDay come Category 1 as an example?

If so, you could read the Description value of your enum

public static string Descricao(this System.Enum itemEnum)
{
   DescriptionAttribute descricao = (DescriptionAttribute)itemEnum.GetType()
        .GetField(itemEnum.ToString())
          .GetCustomAttributes(typeof(DescriptionAttribute), false)
        .FirstOrDefault();
   string retorno = itemEnum.ToString();
   if (descricao != null)
       retorno = descricao.Description;
   return retorno;
}

In a enum

public enum Carro
{
    [Description("Carro Mercedes")]
    Mercedes,
    [Description("Carro Ferrari")]
    Ferrari,
    [Description("Carro Jaguar")]
    Jaguar,
    Porche
}

An example usage with lambda

Carro[] carros = new []{ Carro.Ferrari, Carro.Jaguar };
string texto = Carro.Ferrari.Descricao();
// texto = "Carro Ferrari"
if (carros.Any(c => c.Descricao() == texto))
    Console.WriteLine("Correto");
else
    Console.WriteLine("Incorreto");

// Resultado: Correto
    
09.12.2016 / 18:05
0

So I understand the problem is that you have an "enum" with strings.

1st point: If you do not explicitly tell the value of that enum it takes the order of posting and infers numbers

Example:

enum Meses:short
{
    Janeiro,
    Fevereiro,
    ...
}

In this case the values would be 0, 1, and so on.

The best solution I can give you is to map this enum to a dictionary or else create a enum class with the strings you want to compare. Another solution would be to create an auxiliary table in the bank to have those values registered there, in case they can be changed or added and not leave that enum hard coded .

    
09.12.2016 / 16:08