简体   繁体   English

从SQL查询C#实体框架将int转换为枚举

[英]Cast int to enum from sql query C# Entity Framework

How to return IEnumerable with NewUnitPhaseStatus as an enum? 如何使用NewUnitPhaseStatus作为枚举返回IEnumerable

In SQL the value "NewUnitPhaseStatus" is stored as an int. 在SQL中,值“ NewUnitPhaseStatus”存储为int。 If I just return the query, then when I try to extract the value it just says that the DBcontext has been disposed. 如果我只是返回查询,那么当我尝试提取该值时,它只是说DBcontext已被处置。 But If I try to convert the query result into a list, then it says that int cannot be converted to type NewUnitPhaseStatus(the enum type). 但是,如果我尝试将查询结果转换为列表,则表示无法将int转换为类型NewUnitPhaseStatus(枚举类型)。

public IEnumerable<UnitPhaseLog> GetAllForUnitPhase(long unitPhaseId)
{
    using (var db = new DbContext(_connStringKey))
    {
         var querys = from s in db.UnitPhaseLogs 
                      where s.UnitPhaseId == unitPhaseId 
                      select s;

          return querys;
     }
}

If one uses a foreach statement to convert each row into an enum, they get an error because var query is of class UnitPhaseLog with NewUnitPhaseStatus equal to enum. 如果使用foreach语句将每一行转换为一个枚举,则它们会出错,因为var查询属于UnitPhaseLog类,而NewUnitPhaseStatus等于枚举。

Error message: 错误信息:

If one tries to convert the results to a list. 如果尝试将结果转换为列表。

The 'NewUnitPhaseStatus' property on 'UnitPhaseLog' could not be set to a 'System.Int64' value. 无法将“ UnitPhaseLog”上的“ NewUnitPhaseStatus”属性设置为“ System.Int64”值。 You must set this property to a non-null value of type 'Core.UnitPhaseStatus'. 您必须将此属性设置为'Core.UnitPhaseStatus'类型的非空值。

If one tries to just return the query results themselves: 如果尝试只返回查询结果本身:

The operation cannot be completed because the DbContext has been disposed. 由于已处置DbContext,因此无法完成该操作。

Code: 码:

public enum UnitPhaseStatus
{
    [Display(Name = "No Status")]
    NoStatus,
    [Display(Name = "Not Ready")]
    NotReady,
    [Display(Name = "Materials Needed")]
    MaterialsNeeded,
    [Display(Name = "Ready For Work")]
    ReadyForWork,
    [Display(Name = "Work In Progress")]
    WorkInProgress,
    [Display(Name = "Ready")]
    ReadyForQc,
    [Display(Name = "Approved")]
    Approved,
    [Display(Name = "Rejected")]
    Rejected,
}

You need to cast the return value from the database to the enum type. 您需要将数据库的返回值强制转换为枚举类型。

unit.NewUnitPhaseStatus = (UnitPhaseStatus)UnitPhaseStatus;

Though, you can do this directly, instead of having to go through an extra local variable. 不过,您可以直接执行此操作,而不必经历额外的局部变量。

So, instead of: 因此,代替:

UnitPhaseStatus UnitPhaseStatus = query.NewUnitPhaseStatus;
unit.NewUnitPhaseStatus = UnitPhaseStatus;

You can use: 您可以使用:

unit.NewUnitPhaseStatus = (UnitPhaseStatus)query.NewUnitPhaseStatus;

This is assuming you're using Entity Framework, so if you're not doing so feel free to ignore. 这是假设您使用的是Entity Framework,因此如果您不这样做,请随时忽略。

Rather than worry about casting an Int to an enum, or an enum to an Int, a better solution might be to change the Entity to bind that column directly to the enum and letting the framework do the conversion for you. 不必担心将Int转换为枚举,也不必担心将枚举转换为Int,更好的解决方案可能是更改Entity以将该列直接绑定到枚举,并让框架为您完成转换。

There is no need for you to mess with that foreach loop - your querys is already of the right type, so if it isn't null , just return that. 您无需弄乱那个foreach循环-您的querys已经是正确的类型,因此,如果它不为null ,则只需返回该值即可。

public IEnumerable<UnitPhaseLog> GetAllForUnitPhase(long unitPhaseId)
{
    using (var db = new DbContext(_connStringKey))
    {
        var querys = from s in db.UnitPhaseLogs where s.UnitPhaseId == unitPhaseId select s;

        List<UnitPhaseLog> UnitPhaseLogList = new List<UnitPhaseLog>();
        if (null == querys) return UnitPhaseLogList;

        return querys;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM