I asked a previous question about mapping an enumerated value on a table using LinqToSql and the answer was to use the O/R Designer to set the type to global::MyNamespace.TMyEnum.
That works OK if your enumeration is based on an integer. But what if your enum is based on a string value? The most obvious application of this is:
public enum TGender {
Male,
Female,
Unknown,
}
where the equivalent values on the database are M, F and U. You can’t type an enumeration as a string, so how can you make the Gender property of your LinqToSql object return a TGender value?
I’d add a getter property to my LINQ to SQL object that looks something like this:
Or, if you don’t want to use a huge switch statement, you can add an attribute to your enum, that has a string property that represents the string value in the database for that value. Then, use reflection, get the correct enum, and return that.
Here’s a sample of doing it using Reflection and Attributes:
To test it out:
This results in “Male”.