如何在LinqToSQL查询中使用我的枚举? [英] How do I use my enumeration in a LinqToSQL query?

查看:102
本文介绍了如何在LinqToSQL查询中使用我的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库表中有一个字段用于存储枚举值,例如:

 创建表MyTable(
...
状态tinyint不为空,
...

在我的C#类中我有

  public enum TStatus:byte {
Pending = 1
Active = 2,
Inactive = 3,
}

public TStatus MyStatus {
get {return(TStatus)Status; }
set {Status =(byte)value; }
}

现在我想编写一个使用 MyStatus 属性 MyTable 例如

  var q = MyDataContext.GetTable< MyTable>()。其中​​(t => t.MyStatus == TStatus.Active); 

但当然,Linq不知道如何解释 MyStatus 作为SQL。
为了使其在LinqToSQL中工作,我需要执行哪些 MyStatus

解决方案

查看此链接:



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



由于链接死亡 - 至少对我来说,这个死了 - 这里是重要的部分:


默认情况下,将列添加到实体中时,Type将作为int(System.Int32)出现,但可以将其更改为完全限定类型的枚举(在我的情况下,ConsoleApplication1.CustomerType)。但是,为了完全找到它,你必须添加全局标识符,如下所示:global :: ConsoleApplication1.CustomerType,所以键入那个(但是等同于你的命名空间)到文本框中



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

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 property of MyTable e.g.

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 to MyStatus in order for it to work in LinqToSQL?

解决方案

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

这篇关于如何在LinqToSQL查询中使用我的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆