gpt4 book ai didi

c# - 如何在 LinqToSQL 查询中使用我的枚举?

转载 作者:行者123 更新时间:2023-11-30 20:14:02 24 4
gpt4 key购买 nike

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

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

在我的 C# 课上我有

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

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

现在我想编写一个使用 MyTableMyStatus 属性的 Linq 查询,例如

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

当然,Linq 不知道如何将 MyStatus 解释为 SQL。我需要对 MyStatus 做些什么才能使其在 LinqToSQL 中工作?

最佳答案

查看此链接:

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

随着链接的消失——至少对我来说,这个链接确实消失了——这里是重要的部分:

[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

关于c# - 如何在 LinqToSQL 查询中使用我的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com