Why am i being told my model property (id) doesn't exist, when it does?

I have a model property RegimeItemID which I am using in my controller. However, when I am trying to call it, it is giving an error that it doesnt exist. What have I done wrong?

Controller

public ActionResult ExerciseIndex(int? id, UserExerciseViewModel vmodel)
        {
            User user = db.Users.Find(id);
            //user.RegimeItems = ChosenExercises();

            UserExerciseViewModel model = new UserExerciseViewModel { AvailableExercises = GetAllExercises(), RequestedExercises = ChosenExercises(user, vmodel) };
            //user.RegimeItems = db.RegimeItems.Find(model.SelectedExercise);
            user.RegimeItems = model.RequestedExercises;
            return View(model);
        }

private List<RegimeItem> ChosenExercises(User user, UserExerciseViewModel model)//RegimeItem regimeItem)//User user, RegimeItem regimeItem)
        {
            return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID).ToList();
        }

Models

 public class User
    {
        public int UserID { get; set; }
        public ICollection<RegimeItem> RegimeItems { get; set; }
        public User()
        {
            this.RegimeItems = new List<RegimeItem>();
        } 
    }
    public class RegimeItem
    {
        public int RegimeItemID { get; set; }
        public Exercise RegimeExercise { get; set; }
    }

ViewModel

public class UserExerciseViewModel
{
    public List<Exercise> AvailableExercises { get; set; }
    public List<RegimeItem> RequestedExercises { get; set; }
    public int? SelectedExercise { get; set; }
    public int[] AvailableSelected { get; set; }
    public int[] RequestedSelected { get; set; }
    public string SavedRequested { get; set; }
}

I am getting the error:

'System.Collections.Generic.ICollection<Project.Model.RegimeItem>' does not contain a definition for 'RegimeItemID' and no extension method 'RegimeItemID' accepting a first argument of type 'System.Collections.Generic.ICollection<Project.Model.RegimeItem>' could be found (are you missing a using directive or an assembly reference?)

I am getting it on this line in the controller:

return db.Users.Where(r => r.RegimeItems.RegimeItemID == user.UserID).ToList();

Jon Skeet
people
quotationmark

RegimeItems is a collection of RegimeItem elements - it's not a single element, so it doesn't have a single ID.

It sounds like you may want something like:

return db.Users
         .Where(r => r.RegimeItems.Any(ri => ri.RegimeItemID == user.UserID))
         .ToList();

That will find users who have any regime items with a regime item ID equal to their user ID. It's not really clear whether that's a sensible query though - why would a regime item ID be equal to a user ID? It also doesn't have the right return type - that's going to return a list of Users, not RegimeItems.

It seems more likely that you want something like:

return db.Users
         .Where(u => u.UserID == user.UserID)
         .SelectMany(u => u.RegimeItems)
         .ToList();

Or possibly:

return db.Users
         .Where(u => u.UserID == user.UserID)
         .Single()
         .RegimeItems
         .ToList();

(You should look at the queries involved in each case.)

people

See more on this question at Stackoverflow