将 class 对象的 IQueryable 从视图发送到控制器 class

Send IQueryable of class object from view to controller class

我在 MVC 应用程序中从视图到控制器的 return IQuerable 对象上遇到错误

public partial class Course_Services
{
    #region Process All Courses Application's URL
    public IQueryable<CourseInstanceModel> ProcessAllCoursesApplicationURL(CourseApplicationsURLFeed_Model _obj)
    {
        IQueryable<CourseInstanceModel> ListOfCoursesInstances; 

        //get all the courses which have Application URL is Null..
        using(var _uof = new Courses_UnitOfWork())
        {
            ListOfCoursesInstances = _uof.CourseInstances_Repository.GetAll();

          var _listOfCoursesWithoutURL = from b in ListOfCoursesInstances 
                                           where b.ApplicationURL == null
                                           select ListOfCoursesInstances;

          return _listOfCoursesWithoutURL;
        }
    }
    #endregion
}

我很难理解我哪里做错了。非常感谢

select ListOfCoursesInstances

您刚刚为 select 子句中的每个元素返回了整个源集合。

正如错误试图告诉你的那样,这给出了一个集合的集合,这不是你想要的。

您可能想要 select 原始项目 (b)

你select错了。您应该 selecting 您在那里创建的 b 变量。

var _listOfCoursesWithoutURL = from b in ListOfCoursesInstances 
                               where b.ApplicationURL == null
                               select b;

你现在拥有它的方式是 select ListOfCoursesInstances 该列表中的每个元素一次。由于 ListOfCourseInstances 本身是一个 IQueryable<CourseInstanceModel>,这意味着您要返回一个 IQueryable<IQueryable<CourseInstanceModel>>,这对于您拥有的方法无效,它仅 returns IQueryable<CourseInstanceModel> .

或者,您也可以按照@Steve 在评论中写的,即:

return ListOfCoursesInstances.Where(x => x.ApplicationURL == null);

这使用 LINQ 的扩展方法而不是常规语法来做同样的事情。这只是个人喜好的问题。

public partial class Course_Services
{
    #region Process All Courses Application's URL
    public List<CourseInstanceModel> ProcessAllCoursesApplicationURL(CourseApplicationsURLFeed_Model _obj)
    {

        //get all the courses which have Application URL is Null..
        using(var _uof = new Courses_UnitOfWork())
        {
          var  ListOfCoursesInstances = _uof.CourseInstances_Repository.GetAll();

          var _listOfCoursesWithoutURL = (from b in ListOfCoursesInstances 
                                           where b.ApplicationURL == null
                                           select b).ToList();


          return _listOfCoursesWithoutURL;
        }

    }
    #endregion
}