Zahid Nisar
Zahid Nisar

Reputation: 47

Cannot implicitly convert type 'System.Linq.IQueryable?

here is my code which is giving me above error

    public ActionResult  Edit(int id = 0)
    {
        KBS_Virtual_TrainingEntities db = new KBS_Virtual_TrainingEntities();
        UsersContext ctx = new UsersContext();
        UserProfile model = ctx.UserProfiles.Find(id);

        List<CourseSubscription> user_Course_subscriptions = new List<CourseSubscription>();



        foreach (UserSubscription sub in db.UserSubscriptions)
        {
            if (sub.ID == id)
            {
                user_Course_subscriptions.Add(sub.CourseSubscription);
            }
        }
        List<CourseSubscription> not_subscribe = db.CourseSubscriptions.Except(user_Course_subscriptions);

        var coursesList = from courses in not_subscribe
                          select new SelectListItem
                          {
                              Text = courses.Course.Name,
                              Value = courses.Course.ID
                              .ToString()
                          };
        var CoursesTYPE = from CourseTypes in db.CourseTypes.ToList()
                          select new SelectListItem
                          {
                              Text = CourseTypes.Name,
                              Value = CourseTypes.ID
                              .ToString()
                          };

        ViewBag.CourseID = coursesList;
        ViewBag.type = CoursesTYPE;
        return View(model);

    }

I am trying to find Course Subscription that are not subscribe by the current user by using the above code but its not working?

Upvotes: 3

Views: 11477

Answers (1)

McAden
McAden

Reputation: 13972

You're missing ToList on the results from your Except function. Do a ToList like so:

List<CourseSubscription> not_subscribe = db.CourseSubscriptions.Except(user_Course_subscriptions).ToList();

Or since in your code you're not doing anything that needs a list, simply var it or assign the correct type to it such as IQueryable<CourseSubscription> to it.

var not_subscribe = db.CourseSubscriptions.Except(user_Course_subscriptions);

Upvotes: 2

Related Questions