0

I am trying to filter one of my enum fields from my model. Here's the definition of my enum:

public enum ProjectDifficulty
{
    Medium,
    High,
    [Display(Name = "Very High")]
    VeryHigh,
    Complex
}

But I cannot get the results; I see this error:

Cannot convert from 'System.Linq.IQueryable<Finder.Models.ProjectDifficulty>' to 'System.Collections.Generic.IEnumerable<string>'

My controller contains this code:

var ProjectDifficultyLst = new List<string>();

var ProjectDifficultyQry = from b in db.Projects
                           orderby b.ProjectDifficulty
                           select b.ProjectDifficulty;

ProjectDifficultyLst.AddRange(ProjectDifficultyQry.Distinct());

ViewBag.projectDifficulty = new SelectList(ProjectDifficultyLst);

if (!String.IsNullOrEmpty(projectDifficulty))
{
    SearchQry = SearchQry.Where(l => l.ProjectDifficulty == projectDifficulty);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Have you tried cast it to list? If you want to operate on list you should do ProjectDifficultyQry.Distinct().ToList() – maque May 10 '15 at 17:48
  • Are you not trying to add an IQueryable of enum to a List? – ste-fu May 10 '15 at 21:00
  • @maque I have tried casting to the list but its still showing error saying that invalid arguments. Basically, I am trying to use get the data from the DB and use it as a drop-down search list. But, I am not sure why I am reciving this error about IQueryable. Although, I have another enum in my model and that works fine with search list without any problem. – Harsh Panchal May 10 '15 at 22:10
  • `ProjectDifficulty` is typeof `enum`, not `string`. Hard to understand what your trying to do here. –  May 10 '15 at 23:13

1 Answers1

3

The error message clearly tells you what is wrong. Your ProjectDifficultyQry is of type IQueryable<ProjectDifficulty>, and you want to add it to a List<string>. Of course, you cannot do that. You need to convert your enum values to string. Here's how you do it:

var ProjectDifficultyQry = from b in db.Projects
                           orderby b.ProjectDifficulty
                           select b.ProjectDifficulty.ToString();

ProjectDifficultyLst.AddRange(ProjectDifficultyQry.Distinct());

By the way, you don't have to use ToList(). IQueryable is IEnumerable (See here).

One more thing; I don't know how you are using that DisplayAttribute in your Enum. But, if you want to use "Very High" instead of "VeryHigh" in your SelectList, check this.

Community
  • 1
  • 1
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • Hi @ataravai, thanks for your reply. I have managed to get it working by using 'ViewBag.projectDifficulty = new SelectList(ProjectDifficultyQry.Distinct());' however, it does not display all of the fields, it only display medium, high and very high but there is another field as well in the table called Complex but it does not showing in drop down box. Also, I am trying to filter according to user selection any advice on that?? I am thinking to do something like this 'SearchQry = SearchQry.Where(x => x.ProjectDifficulty = projectDifficulty);' – Harsh Panchal May 11 '15 at 13:49
  • @HarshPanchal, do you want all the Project Difficulties to be added to the SelectList or only those that are in the database? If you want all of them, then, why are you querying the database? – ataravati May 11 '15 at 14:24
  • I need Project Difficulties as a SelectList so user can filter based on the selected Project Difficulty. – Harsh Panchal May 11 '15 at 22:07
  • @HarshPanchal, you didn't get my question. I know you need it as a SelectList. But, why are you querying the database? Don't you need all the options in your Enum in the SelectList? – ataravati May 12 '15 at 00:29