将IQueryable列表转换为通用列表?

Mar*_*uta 3 c# asp.net-mvc linq-to-entities

我正在尝试调用GetScanningLogOnSettings(),查询ScanningDepartments表以获取部门,然后创建实例ApplicationLogOnModel,将查询结果分配给其中的变量ApplicationLogOnModel,并返回结果.

private ApplicationLogOnModel GetScanningLogOnSettings()
   {
       var mesEntity = new MESEntities();
       var departments = from dept in mesEntity.ScanningDepartments
                         select dept.Department.ToList();

       ApplicationLogOnModel depts = new ApplicationLogOnModel()
       {
           Department = departments
       };

       return depts;
   }
Run Code Online (Sandbox Code Playgroud)

它给了我:

"无法隐式转换 'System.Linq.IQueryable<System.Collections.Generic.List<char>>'System.Collections.Generic.List<Models.Department>'

尝试转换为列表,我遇到了一些麻烦.

Dan*_*rth 7

你遗漏了一些括号:

var departments = (from dept in mesEntity.ScanningDepartments
                   select dept.Department).ToList();
Run Code Online (Sandbox Code Playgroud)

你的代码调用ToList()dept.Department,而不是整个查询.