Mark Saluta
Mark Saluta

Reputation: 587

Convert IQueryable List to Generic List?

I am trying to call GetScanningLogOnSettings(), which queries the ScanningDepartments table to get the departments, then creates an instance of ApplicationLogOnModel, assigns the queried results to a variable within the ApplicationLogOnModel, and returns the result.

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;
   }

It gives me:

"Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<char>> to 'System.Collections.Generic.List<Models.Department>'

Tried converting to lists and am having a little trouble.

Upvotes: 3

Views: 6672

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174279

You are missing some parentheses:

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

Your code calls ToList() on dept.Department and not on the whole query.

Upvotes: 7

Related Questions