Is there a way how to filter data with an enum property using a string?
This is my function in service layer which takes 2 arguments for paging feature and 3rd argument is for filtering projects by their status.
I want to do something like this projects.Where(x => x.Status == status) but it throws error because I cannot compare enum with string. Is there some workaround for this?
public async Task<ListResult<ProjectDTO>> GetListedProjects(int pageSize, int pageNumber, string status)
{
var projects = await unitOfWork.ProjectRepository.Get();
//i cannot filter like this
projects.Where(x => x.Status == status);
var orderedProjects = projects.OrderBy(x => x.Name);
var projectList = orderedProjects.ToPagedList(pageNumber, pageSize);
var data = projectList.Select(x => ToDTO.ProjectBuild(x)).ToList();
return new ListResult<ProjectDTO> { Data = data, TotalCount = projectList.TotalItemCount };
}
Here is my project model:
public class Project : ManagementBaseClass
{
[Key]
public int Id { get; set; }
public Status Status { get; set; }
public Priority Priority { get; set; }
//etc just deleted more properties to make this cleaner
}
This is my enum which i use for assigning status to projects, tasks etc
public enum Status
{
New = 1,
Active = 2,
OnHold = 3,
Testing = 4,
Finished = 5,
Dropped = 6
}
string status? Why don't You useStatus statusinstead?Status statusparameter in my method. Have to try that.Status.