Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:09:14+00:00 2026-06-07T11:09:14+00:00

I have a class for something simple, Status, and it contains two properties. I

  • 0

I have a class for something simple, Status, and it contains two properties. I want to use an Enum for one of the Properties(StatusID) so that I can eliminate a bunch of Magic Strings.

My question is how I then work with it, for example: I have a Method that returns a List for binding in a dropdown box that looks like this –>

public static IList<Status> GetAdminStatuses()
{
  IQueryable<Status> stat=context.tblAdminStatus
       .Where(s => s.InactiveDate > DateTime.Now || s.InactiveDate == null)
       .Select(s => new Status()
       {
         StatusID=s.StatusID,
         StatusDescription=s.StatusDesc
       });
   return stat.ToList();
}

It obviously does not like my StatusID=s.StatusID part as the DB stores it as a varchar. Am I missing something simple here or have I stumbled into noob territory and should not be doing it this way?

For reference here is the Class and Enum:

public class Status
{
  public string StatusID {get; set;}
  public string StatusDescription {get; set;}
}

public enum MyStatusID
{
  draft, pending, declined, accepted, close 
}

EDIT

So taking the advice here I was able to get my method to compile however at runtime I get the following –> Method 'System.Object Parse(System.Type, System.String)' has no supported translation to SQL.

Thoughts?


EDIT – Method in it’s entirety by request, thanks (NOTE that NoaStatusID == MyStatusID)

   public static IList<Status> GetAdminStatuses(NoaStatusID currentStatus = NoaStatusID.draft)
    {
        using (var context = MemberDataContext.Create())
        {
            IQueryable<Status> stat=context.tblAdminStatus
                   .Where(s => s.InactiveDate > DateTime.Now || s.InactiveDate == null)
                   .Select(s => new Status()
                     {
                       StatusID=NoaStatusID)Enum.Parse(typeof(NoaStatusID),s.StatusID),
                       StatusDescription=s.StatusDesc
                     });

            switch (currentStatus)
            {
                case NoaStatusID.draft:
                    stat=stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending));                                                     
                    break;
                case NoaStatusID.pending:
                    stat = stat.Where(s => (s.StatusID == NoaStatusID.accepted || s.StatusID ==NoaStatusID.declined || s.StatusID ==NoaStatusID.pending));
                    break;                        
                case NoaStatusID.declined:
                    stat = stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending || s.StatusID == NoaStatusID.declined));
                    break;
                case NoaStatusID.accepted:
                    stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.accepted));
                    break;
                case NoaStatusID.mailed:
                    stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal));
                    break;
                case NoaStatusID.monitor:
                case NoaStatusID.appeal:
                case NoaStatusID.close:
                    stat = stat.Where(s => (s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal));   
                    break;                    
            }


            return stat.ToList();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:09:15+00:00Added an answer on June 7, 2026 at 11:09 am

    i believe what you’re searching for is:

    StatusID = (MyStatusID)Enum.Parse(typeof(MyStatusID), s.StatusID),
    

    in .Net 4.0 there is also a Enum.TryParse(string, out enum) but that is not so useful inside your .Select()

    Alternatively:
    albeit less efficient in most cases, you can keep the Status.StatusID as a string and add a readonly property StatusEnum that outputs the Enum value on the fly:

    public MyStatusID StatusEnum {
        get {
            return (MyStatusID)Enum.Parse(typeof(MyStatusID), StatusID)
        }
    
        private set;
    }
    
    in .Net 4.0:
    public MyStatusID StatusEnum {
        get {
            MyStatusID value;
            if(!Enum.TryParse(StatusID, out value)
              value = MyStatusID.Default; // default value, instead of Exception throwing
    
            return value;
        }
    
        private set;
    }
    

    this alternative re-parses the value everytime instance.StatusEnum is read, so I don’t recommend it unless LINQ hates the first approach


    Responding to your last EDIT:

    The Enum.Parse() is translating to SQL fine in your example. The problem is in the switch statement where you’re adding on a .Where() clause that has a comparison with an Enum. LINQ doesn’t know how to turn an Enum == Enum into SQL but it does know to do it with C# objects. So the easiest solution is to ToList() them and do the comparison locally. Unfortunately, that means it’s downloading rows of -all- Status types from the database and then filters them locally. If you have millions of records this may not be reasonable:

       public static IList<Status> GetAdminStatuses(NoaStatusID currentStatus = NoaStatusID.draft)
        {
            using (var context = MemberDataContext.Create())
            {
                List<Status> stat=context.tblAdminStatus
                       .Where(s => s.InactiveDate > DateTime.Now || s.InactiveDate == null)
                       .Select(s => new Status()
                         {
                           StatusID=NoaStatusID)Enum.Parse(typeof(NoaStatusID),s.StatusID),
                           StatusDescription=s.StatusDesc
                         })
                       .ToList();
    
                switch (currentStatus)
                {
                    case NoaStatusID.draft:
                        stat=stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending)).ToList();                                                     
                        break;
                    case NoaStatusID.pending:
                        stat = stat.Where(s => (s.StatusID == NoaStatusID.accepted || s.StatusID ==NoaStatusID.declined || s.StatusID ==NoaStatusID.pending)).ToList();
                        break;                        
                    case NoaStatusID.declined:
                        stat = stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending || s.StatusID == NoaStatusID.declined)).ToList();
                        break;
                    case NoaStatusID.accepted:
                        stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.accepted)).ToList();
                        break;
                    case NoaStatusID.mailed:
                        stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal)).ToList();
                        break;
                    case NoaStatusID.monitor:
                    case NoaStatusID.appeal:
                    case NoaStatusID.close:
                        stat = stat.Where(s => (s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal)).ToList();   
                        break;                    
                }
    
    
                return stat;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm missing something fundamental here. i have a very simple custom class that draws
I have a class Something that implements ISomething . How can I convert/cast from
I have a class that I want to contain multiple objects of something I
Say I have a simple object such as class Something { public int SomeInt
We have a class a class that looks something like the following: public class
I know that I am missing something simple here. I have got this homework
I have the following simple entity: public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID {
I have a feeling I'm missing something very simple here. I have a class
So I have a class is something like this public class Order { //some
I have class Employee which is something like this. class Emp { int EmpID;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.