0

My Model is given as:

public class Department
{
    // just keeping it relevant
    public List<Job> Jobs { get; set; }
}
public class Job
{
    public int ID { get; set; }
    public string Name { get; set; }
}

And I want to select the Jobs that belong to the specific department and convert them into Selectlist so that i can pass them to View.

What I have done in the controller is( yes i am hardcoding the id with value 1):

ViewBag.JobID = new SelectList( db.Departments.Where(x => x.ID == 1).Select(m => m.Jobs, "ID","Name");


But I am getting the following error,
List does not contain a property with the name 'ID'

Rusty
  • 4,138
  • 3
  • 37
  • 45
  • 1
    You don't have matching parenthesis - it should be `.Select(m => m.Jobs), "ID" ...` Is that a typo? –  Aug 02 '15 at 23:29

1 Answers1

1

Jobs returns a List<Job>, which of course does not have a property named ID. If you want to return a List, then you have to use SelectMany(x => x.Jobs).

Some other suggestions:

  • Don't use List, instead use ICollection (reason)
  • Make your navigation properties virtual (reason)
Community
  • 1
  • 1
LueTm
  • 2,366
  • 21
  • 31
  • thnx for suggestions. I am looking into it. – Rusty Aug 03 '15 at 05:13
  • Your answer pointed me in the right direction. The mistake I made is that I didn't include departmentID in the jobs class. Realized it after checking the tables in the database. So now I can directly select from db.jobs and save myself some hassle. – Rusty Aug 03 '15 at 06:20