Reputation: 4024
I have a table
Images
-Id
-Name
-StatusId
and a lookup table:
ImagesStatus
-Id
-Name
what would be the best practice to do a query on the images table based on StatusId?
1) Hardcode the Id:
db.Images.Where(x => x.StatusId == 1);
3) Create an enum (ImagesStatusEnum) that matches the ImagesStatus table elements and then do:
public enum ImagesStatusEnum
{
Pending = 1,
Approved = 2,
Rejected = 3
}
int approvedStatusId = (int)ImagesStatusEnum.Approved;
db.Images.Where(x => x.StatusId == approvedStatusId).ToList();
3) Something else I haven't thought about?
Upvotes: 1
Views: 112
Reputation: 2487
In EF5 and newer you can map a field on an entity to an existing enum. Right click the field in the model (type preferably int) and click convert to enum. You can then setup to use a new enum, or an existing one.
You can do your query like this:
db.Images.Where(x => x.StatusId == ImagesStatusEnum.Approved).ToList();
I prefer the enum way since it gives a bit more readability. The cost of maintaining these enums are minimal.
Upvotes: 3