0

How to return IEnumerable with NewUnitPhaseStatus as an enum?

In SQL the value "NewUnitPhaseStatus" is stored as an int. If I just return the query, then when I try to extract the value it just says that the DBcontext has been disposed. 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).

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.

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. You must set this property to a non-null value of type 'Core.UnitPhaseStatus'.

If one tries to just return the query results themselves:

The operation cannot be completed because the DbContext has been disposed.

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,
}
11
  • 1
    You talking about this line? UnitPhaseStatus UnitPhaseStatus = query.NewUnitPhaseStatus; Commented May 23, 2017 at 20:34
  • 1
    Please show the code that defined UnitPhaseStatus . Your enum values should match the SQL definition. Commented May 23, 2017 at 20:36
  • 1
    Look at @DigiFriend answer. In your enum definition, you have to add = VALUE: NoStatus = 0, NotReady = 1, MaterialsNeeded = 2 WHERE 0,1 and 2 will match the value in your SQL column. Commented May 23, 2017 at 20:41
  • 1
    Any reason you can't simply return querys? Commented May 23, 2017 at 20:45
  • 1
    You ORM (Entity Framework?) does the conversion for you. Commented May 23, 2017 at 20:49

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

The code breaks at the foreach statement because var query is of type NewUnitPhaseStatus, which is an enum
I don't think you follow. OK. What type is UnitPhaseLog.NewUnitPhaseStatus? What type is query.NewUnitPhaseStatus (not the name - the type)? The types need to match. If they do not, you need to cast.
1

This is assuming you're using Entity Framework, so if you're not doing so feel free to ignore.

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.

2 Comments

But then how do you query the results?
If you get around the conversion errors you're getting, you should be able to get the result as a list.
1

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.

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;
    }
}

5 Comments

The 'NewUnitPhaseStatus' property on 'UnitPhaseLog' could not be set to a 'System.Int64' value. You must set this property to a non-null value of type 'Core.UnitPhaseStatus'.
This suggests that your UnitPhaseLog.NewUnitPhaseStatus is a long, when it really should be a Core.UnitPhaseStatus. This is probably where all your problems are coming from.
It is a UnitPhaseStatus. I noticed that if I dont turn the IEnumerable into a list before retrieving data, then it says the data has been disposed.
From my experience, even if I just return queries (like how it is in your answer above) the data isnt accessable because it is disposed.
I showed the class of the UnitPhaseLog.NewUnitPhaseStatus in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.