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();
}
}
i believe what you’re searching for is:
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.StatusIDas a string and add a readonly propertyStatusEnumthat outputs the Enum value on the fly: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 anEnum. LINQ doesn’t know how to turn anEnum == Enuminto 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: