LINQ - Enum column names
- Is it possible to expose the column names of a table you added using LINQ as enums ?For example, i have a table "Customers":ID, intName, varChar(30)Addres, varChar(80)Now i want a class "Customer_Fields" that has ID, Name and Address as enums (based on the linq table).
- Moved byRiquel_Dong Monday, October 26, 2009 3:39 AMone LINQ question (From:Windows Communication Foundation)
Answers
Hi Joeri123,
Welcome to LINQ Project General forum!
Whether we are using LINQ to SQL and LINQ to Entities to access the database, the enum typed properties on the entity classes are feasible. In LINQ to SQL, we can directly set the property in .dbml to type of enum and LINQ to SQL can help us to make the type conversion. However, in the current version of Entity Framework (.NET 3.5 SP1) there is no direct support for enum type. We need to create a separate property to make the type conversion.
For LINQ to SQL, please see
http://dotnet.org.za/hiltong/archive/2008/08/06/using-enums-with-linq-to-sql.aspx
http://msdn.microsoft.com/en-us/library/bb386947.aspx
For LINQ to Entities, please see
http://weblogs.asp.net/alexeyzakharov/archive/2008/09/18/enum-in-ado-net-entity-framework-v1.aspx
Have a nice day!
Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed As Answer byLingzhi SunMSFT, ModeratorWednesday, October 28, 2009 12:48 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:28 AM
Hi Joeri123,
Could you please tell me which LINQ technology are you using? LINQ to SQL, LINQ to Entities, or normal LINQ to Objects?
If you are using LINQ to SQL, the data column in type of TINYINT, SMALLINT, INT, BIGINT or CHAR, NCHAR, VARCHAR, NVARCHAR can all mapped to enum. Actually they are still keep their values. At the client side, we use enum to get/set their values and at the database side, they are still in distinct types. For detail, please see http://msdn.microsoft.com/en-us/library/bb386947.aspx#EnumMapping. If you want to keep the original property types, you can define some custom enum properties mapping to the original properties since the data classes are all partial classes, as this link suggested, http://weblogs.asp.net/alexeyzakharov/archive/2008/09/18/enum-in-ado-net-entity-framework-v1.aspx. Then we don’t need to make another data class.
If you also want to export the column names into a enum type, you can refer to the T4 templates for LINQ to SQL to add some custom code generation process.
Have a nice weekend!
Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:28 AM
- Proposed As Answer byLingzhi SunMSFT, ModeratorThursday, November 05, 2009 12:44 AM
All Replies
- From what I can tell this doesn;t appear to be a WCF question (this is the WCF forum) - did you post to the wrong forum by mistake or am I missing some detail about your question?
Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
Twitter: richardblewett Hi Joeri123,
Welcome to LINQ Project General forum!
Whether we are using LINQ to SQL and LINQ to Entities to access the database, the enum typed properties on the entity classes are feasible. In LINQ to SQL, we can directly set the property in .dbml to type of enum and LINQ to SQL can help us to make the type conversion. However, in the current version of Entity Framework (.NET 3.5 SP1) there is no direct support for enum type. We need to create a separate property to make the type conversion.
For LINQ to SQL, please see
http://dotnet.org.za/hiltong/archive/2008/08/06/using-enums-with-linq-to-sql.aspx
http://msdn.microsoft.com/en-us/library/bb386947.aspx
For LINQ to Entities, please see
http://weblogs.asp.net/alexeyzakharov/archive/2008/09/18/enum-in-ado-net-entity-framework-v1.aspx
Have a nice day!
Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed As Answer byLingzhi SunMSFT, ModeratorWednesday, October 28, 2009 12:48 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:28 AM
- Hi Joeri123,
How is the problem? Do you need any further assistance? If so, please be free to tell me.
Have a great day!
Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Hi,i'm still having the problem.I created a data class using LINQ. I renamed many of the data member, because they have confusing names.Now i want to expose all these 'new' column names as an enum.Example:Table: Name = ResourcesFields: ID, Name, AddressNow i need a enum likeResource_Fields[EnumMember]ID[EnumMember]Name[EnumMember]Address
I understand you can map your data class members directly to enum, but i also want them as their normal values. Would this mean i would have to create two the same data classes, but one as it is, and one with a mapping to the enum type ? Or is there an easier way to do this? Hi Joeri123,
Could you please tell me which LINQ technology are you using? LINQ to SQL, LINQ to Entities, or normal LINQ to Objects?
If you are using LINQ to SQL, the data column in type of TINYINT, SMALLINT, INT, BIGINT or CHAR, NCHAR, VARCHAR, NVARCHAR can all mapped to enum. Actually they are still keep their values. At the client side, we use enum to get/set their values and at the database side, they are still in distinct types. For detail, please see http://msdn.microsoft.com/en-us/library/bb386947.aspx#EnumMapping. If you want to keep the original property types, you can define some custom enum properties mapping to the original properties since the data classes are all partial classes, as this link suggested, http://weblogs.asp.net/alexeyzakharov/archive/2008/09/18/enum-in-ado-net-entity-framework-v1.aspx. Then we don’t need to make another data class.
If you also want to export the column names into a enum type, you can refer to the T4 templates for LINQ to SQL to add some custom code generation process.
Have a nice weekend!
Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:28 AM
- Proposed As Answer byLingzhi SunMSFT, ModeratorThursday, November 05, 2009 12:44 AM
Hi Joeri123,
How is the problem now? Do you need any further assistance? If so, please feel free to let me know.
Have a nice day!Best Regards,
Lingzhi Sun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

