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

3

我正在尝试调用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;
   }

这给了我:

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

尝试转换为列表,但遇到了一些问题。

1个回答

7
您缺少一些括号:
var departments = (from dept in mesEntity.ScanningDepartments
                   select dept.Department).ToList();

您的代码在dept.Department上调用了ToList()而不是整个查询。


现在它显示“无法隐式转换类型'System.Collections.Generic.List<string>'为'System.Collections.Generic.List<Models.Department>。 - Mark Saluta
@MarkSaluta:这是另一个问题,不在本问题的范围内。很可能,dept.Department 返回一个字符串,而 ApplicationLogOnModel.Department 不是字符串列表,而是 Models.Department 实例的列表。 - Daniel Hilgarth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接