c# - 理解枚举——用作常量

标签 c# entity-framework enums

所以我尝试使用枚举,可能以错误的方式,因为我来自 PHP。在 C# 中,我有一个全局类:

public static class GlobalTypes
{
    public enum assignmentType { SERV = "SERV", PROD = "PROD", PER = "PER", }
}

从那里我尝试通过以下方式与实体进行交互:

    public static IEnumerable<Person> getAllAgents(int id)
    {
        using (var db = new LocAppContext())
        {
            var person = (from p in db.Person
                          join la in db.LocationAssignment on p.id equals la.value
                          where la.locationID == id && la.type == GlobalTypes.assignmentType.PER
                          select p).ToList();

            return person;
        }

    }

但是我得到了错误:

Operator '==' cannot be applied to operands of type 'string' and 'LocApp.Helpers.Classes.LocationAssignments.GlobalTypes.assignmentType'

发生在

la.type == GlobalTypes.assignmentType.PER

我的逻辑,这是来自 php,是我想要一个全局常量,我可以在任何地方调用以在调用时“回显”该常量的值,因此常量值可以更改但我不必在一百万个地方改变它。

想法?

最佳答案

如果你想要常量字符串,那么不要使用枚举。枚举用于整数类型。只需使用常量:

public static class MyClass
{
    public const string PROD = "PROD";
    public const string DEV = "DEV";
}

// elsewhere...
la.type == MyClass.PROD;

关于c# - 理解枚举——用作常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16178429/

相关文章:

entity-framework - SQLite 数据库放置在 UWP 应用中的位置

java - 无法使用私有(private)变量对非静态字段 memberVariable 进行静态引用

用户/Web 控件设计时支持的 C# 枚举列表/集合?

enums - 如何在枚举中保留值类型

c# - 如何使用 Entity Framework 从另一个类返回 ID 或全部?

内存中的 C# 数组比预期大

c# - 将字节数组转换为托管结构

c# - 设置 ComboBox 项目高度

c# - C#多线程访问单一方法

c# - Linq-to-SQL 中的 SubmitChanges 和 Entity Framework 中的 SaveChanges 之间的技术区别是什么?