2013-02-19 70 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