3

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?

Zahid Nisar
  • 47
  • 1
  • 1
  • 5
  • telling at which line of code the exception was thrown exactly may help – har07 Mar 10 '14 at 05:58
  • List not_subscribe = db.CourseSubscriptions.Except(user_Course_subscriptions); – Zahid Nisar Mar 10 '14 at 05:59
  • posting complete exact error message also helpful, like this one (possible duplicate btw) : [Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'](http://stackoverflow.com/questions/3402822/cannot-implicitly-convert-type-system-linq-iqueryable-to-system-collections-g) – har07 Mar 10 '14 at 06:04
  • Error 327 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) this error I am getting – Zahid Nisar Mar 10 '14 at 06:08
  • yep, check answer given in the link above. It is almost identical to your problem (only difference is `List` here vs `IList` in the other post). And of course check answer from @McAden – har07 Mar 10 '14 at 06:12

1 Answers1

2

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);
McAden
  • 13,714
  • 5
  • 37
  • 63