I have the following situation in LINQ: I have an object (a blog "Post") that has a "Status" field which can take on different values, and there's a table listing all these values (this table just has Id, Desc).
Now, since the relationship between both tables is created, in the Model, Post.Status is not an int, but an instance of "PostStatus".
Thus, if I want to set the status of a Post to, say, 20, I can't just set the field to 20, I need to have an instance of PostStatus to assign. These status values are all hardcoded with the logic, so it's just fine to have "20" hardcoded in my code.
Is there a better way to do it that this?
switch (Action) {
case "Ignore":
post.PostStatus = (from s in db.PostStatus where s.Id == 90 select s).First();
break;
case "Assign":
post.PostStatus = (from s in db.PostStatus where s.Id == 20 select s).First();
break;
case "MarkDone":
post.PostStatus = (from s in db.PostStatus where s.Id == 30 select s).First();
break;
case "MarkPublished":
post.PostStatus = (from s in db.PostStatus where s.Id == 40 select s).First();
post.Public = true;
break;
}
I hate this code, honestly, first of all because it needlessly queries the DB to get a PostStatus instance, but mostly because it's just so verbose.
Is there a better way to do this?
Thanks!
Daniel