3

I have an enum with a whole bunch of values (only three shown here):

public enum LookupType
{
   Alignment,
   Language,
   ReEmbedBehavior
}

I then have a method that gets data based on a varchar field called LookupType...what I want is to limit callers of this method to only the lookuptypes that are in the database...so at the end of my WHERE clause, I want the enum name as a string, not the integer value of it.

Callers would then do something like GetLookupsByLookupType(LookupType.Language) and my method would make the call as "where lookuptype = 'Language'"

public List<Lookup> GetLookupsByLookupType(UnicornMedia.Core.DomainModel.Enumerations.LookupType lookupType)
        {
            var lookups = new List<Lookup>();
            string SQL = String.Format(@"Select id, name, value, lookuptype
                                         from lookups
                                         where lookuptype = '{0}'", lookupType.ToString());

            ...<snip>...                                         
        }

It's probably something simple but I seem to bump into this from time to time and instead of figuring it out, I end up just using a Dictionary...anyway, there it is, thanks

J Benjamin
  • 4,722
  • 6
  • 29
  • 39
  • 2
    Are you sure ToString() doesn't give you the name? – Benjol Dec 03 '10 at 22:17
  • no I'm not sure but as it happens I'm not going to be able to debug or test before I check this in so I need to make sure I get it right – J Benjamin Dec 03 '10 at 22:20
  • 1
    not test or debug before you check it in? that doesn't sound right at all.... – F.B. ten Kate Dec 03 '10 at 22:23
  • 1
    @JBenjamin, trust me, it works :) – Benjol Dec 03 '10 at 22:24
  • thanks all! Feel kinda dumb that I didn't think of just testing it in a console app...live and learn tho, get smarter every day :) – J Benjamin Dec 03 '10 at 22:46
  • @JBenjamin, but beware that this can bite you badly if your ToString is not in the same assembly as your enum (http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/311831#311831) – Benjol Dec 03 '10 at 22:57
  • 1
    Check out some tool like SnippetCompiler (http://www.sliver.com/dotnet/SnippetCompiler/) - lets you take a short snippet of C# and try it out without all the overhead of Visual Studio! Absolutely priceless... – marc_s Dec 04 '10 at 09:00

6 Answers6

8

Simply doing a .ToString() will get you the enum name as a string value.

F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
2

Have you tried Enum.GetName?

Actually, the following snippet shows that simply calling ToString works as well.

enum LookupType {
    Language
}
public class Program {
    public static void Main(string[] args) {
        string str = string.Format("{0}", LookupType.Language);
        // str = "Language"
        Console.WriteLine(LookupType.Language);
        // output: Language
    }
}
Patrick
  • 17,669
  • 6
  • 70
  • 85
2

Try the following

string name = System.Enum.GetName(typeof(LookupType), LookupType.Language);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

You can use the GetName method (http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx):

Enum.GetName(typeof(LookupType), lookupType);
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

You do not need GetName...

LookupType.Alignment.ToString();

or

just as you have it in your code...

lookupType.ToString()
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
1

Your code should work just fine. I ran the following and the string returned the expected results.

class Program
{
    static void Main(string[] args)
    {
        LookupType lookupType = LookupType.Language;

        Console.WriteLine(GetLookupsByLookupType(lookupType));

        Console.Read();
    }

    public static string GetLookupsByLookupType(LookupType lookupType)
    {
        string SQL = String.Format(@"Select id, name, value, lookuptype from lookups where lookuptype = '{0}'", lookupType.ToString());

        return SQL;                  
    }

}

public enum LookupType
{
    Alignment,
    Language,
    ReEmbedBehavior
}

Make sure you are not passing your SQL string as you have it displayed above. Either put the string together on one line, or use the following:

        string SQL = String.Format(@"Select id, name, value, lookuptype from " + 
            "lookups where lookuptype = '{0}'", lookupType.ToString());
IAbstract
  • 19,551
  • 15
  • 98
  • 146