2

I have values being returned as strings from a database that have names that make them unsuitable for enum names, e.g. "Color.Red". How do I name my enums something else but then equate them to these string values when it comes to writing conditional logic?

public enum ItemColor
{
      Red,
      Green,
      Yellow    
}

var dbList = new List(){"Color.Red", "Color.Green", "Color.Yellow"}


if (dbList.Contains(ItemColor.Red)))
{
   //do something
}

Is there some way that I can easily decorate the enum with an additional string value equal to what I am returning from the database?

Sperick
  • 2,691
  • 6
  • 30
  • 55
  • `var newList = dbList.Select(Enum.Parse).ToList();` maybe? Then you have it as the enums rather than strings. – Charlieface Jul 11 '23 at 15:36
  • 1
    How are you fetching data from the DB? If you're using EF Core, it has [a way](https://stackoverflow.com/questions/47721246/ef-core-2-0-enums-stored-as-string) to specify a converter. – Brian Jul 11 '23 at 15:55

4 Answers4

2

No there is nothing build-in. You can always write your own method with custom attribute. Something to get you started:

static class CustomEnumNameGetter
{
    // "cache" reflection here
    private class Internal<T> where T: struct, Enum
    {
        public static readonly Dictionary<T, string> Dictionary;

        static Internal()
        {
            var customPrefixAttribute = typeof(T).GetCustomAttribute<CustomPrefixAttribute>();
            var prefix = customPrefixAttribute?.Name ?? string.Empty;
            Dictionary = Enum.GetValues<T>()
                .ToDictionary(v => v, v => prefix + Enum.GetName(v));
        }
    }

    public static string GetName<T>(T value) where T: struct, Enum => Internal<T>.Dictionary.TryGetValue(value, out var name)
        ? name
        : string.Empty;
}

class CustomPrefixAttribute : Attribute
{
    public string Name { get; }

    public CustomPrefixAttribute(string name)
    {
        Name = name;
    }
}

and usage:

[CustomPrefix("Color.")]
public enum ItemColor
{
    Red,
    Green,
    Yellow    
}

if (dbList.Contains(CustomEnumNameGetter.GetName(ItemColor.Green)))
{
    //do something
} 

Or just concatenate prefix in-place (i.e. if (dbList.Contains($"Color.{ItemColor.Red}"))).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

Use

if (dbList.Contains("Color." + ItemColor.Red))

The expression "Color." + ItemColor.Red evaluates to "Color.Red".

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Use the following conversion method:

private static string ToString(ItemColor itemColor)
{
    return "Color." + itemColor;
}

To use:

if (dbList.Contains(ToString(ItemColor.Red)))
{
    // do something
}
Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
0

You can to use Split to find Part of Enum list(Green,Yellow)

Then you can get enum(with Enum.TryParse)

finally, compare between Enum(ItemColor ) and enum list(myItemColor)

var dbList = new List<string>() { "Color.Green", "Color.Yellow" };


if (dbList.Any(d => Enum.TryParse(d.Split('.')[1], out ItemColor myItemColor) && myItemColor==ItemColor.Red) )
{
    
}
abolfazl sadeghi
  • 2,277
  • 2
  • 12
  • 20