2013-02-19 72 views
3

我试图拨打GetScanningLogOnSettings(),查询ScanningDepartments表获取部门,然后创建ApplicationLogOnModel的实例,将查询的结果分配给ApplicationLogOnModel中的变量,并返回结果。将IQueryable列表转换为通用列表?

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>'

试图转换到列表时遇到了一点小麻烦

回答

7

是你。缺少一些括号:

var departments = (from dept in mesEntity.ScanningDepartments 
        select dept.Department).ToList(); 

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

+0

它现在说“无法隐式转换类型‘System.Collections.Generic.List 为’System.Collections.Generic.List 2013-02-19 16:01:13

+0

@MarkSaluta:这又是一个问题,而不是这个问题的范围。最有可能的是,'dept.Department'返回一个字符串,'ApplicationLogOnModel.Department'不是一个字符串列表,而是'Models.Department'实例。 – 2013-02-19 16:04:22