无法隐式转换类型'System.Linq.IQueryable?

时间:2014-03-10 05:48:41

标签: c# linq asp.net-mvc-4

这是我的代码,它给了我上面的错误

    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);

    }

我试图通过使用上面的代码找到当前用户没有订阅的课程订阅,但它不起作用?

1 个答案:

答案 0 :(得分:1)

您在ToList功能的结果中遗漏了Except。像这样ToList

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

或者,因为在您的代码中,您没有做任何需要列表的内容,只需var或者为其指定正确的类型,例如IQueryable<CourseSubscription>

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