How do I use my enumeration in a LinqToSQL query?

前端 未结 2 1266
有刺的猬
有刺的猬 2021-01-03 15:08

I have a field in my database table that use to store an enumeration value, e.g.:

create table MyTable (
  ...
  Status tinyint not null,
  ...
)


        
相关标签:
2条回答
  • 2021-01-03 15:27

    Don't have a compiler handy, but I think if you cast your enum to an int, it will work. So try:

    var q = MyDataContext.GetTable().Where(t => t.MyStatus == (int)TStatus.Active);

    0 讨论(0)
  • 2021-01-03 15:36

    Check out this link:

    http://dotnet.org.za/hiltong/archive/2008/08/06/using-enums-with-linq-to-sql.aspx

    As links die - and at least for me this one did die - here is the important part:

    [When adding the column to the entity] by default, the Type will come up as an "int (System.Int32)", but you can change it to the fully-qualified type of the enum (in my case, ConsoleApplication1.CustomerType). BUT, in order to locate it fully, you have to add the global identifier, as follows: global::ConsoleApplication1.CustomerType , so type that as is (but the equivalent for your namespace) into the textbox

    0 讨论(0)
提交回复
热议问题