How to use my enum in a LinqToSQL query?

I have a field in my database table that is used to store an enumeration value, for example:

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

and in my c # class i have

 public enum TStatus : byte { Pending = 1 Active = 2, Inactive = 3, } public TStatus MyStatus { get { return (TStatus)Status; } set { Status = (byte)value; } } 

now I want to write a Linq query that uses the MyStatus MyTable property, for example.

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

but of course Linq doesn't know how to interpret MyStatus as SQL. What do I need to do for MyStatus to work in LinqToSQL?

+2
source share
2 answers

Check out this link:

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

How the links die - and at least it died for me - this is the important part:

[When adding a column to the entity] by default the type will be displayed as "int (System.Int32)", but you can change it to a fully defined type of enumeration (in my case, ConsoleApplication1.CustomerType). BUT, to find it completely, you need to add a global identifier as shown below: global :: ConsoleApplication1.CustomerType, so type in the text box (as an equivalent for your namespace)

+7
source

You do not have a compiler, but I think that if you list your enum in int, it will work. So try:

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

0
source

Source: https://habr.com/ru/post/1433591/


All Articles