加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将IQueryable列表转换为通用列表?

发布时间:2020-12-16 01:53:08 所属栏目:百科 来源:网络整理
导读:我试图调用GetScanningLogOnSettings(),它查询ScanningDepartments表以获取部门,然后创建ApplicationLogOnModel实例,将查询结果分配给ApplicationLogOnModel中的变量,并返回结果. private ApplicationLogOnModel GetScanningLogOnSettings() { var mesEntity
我试图调用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;
   }

它给了我:

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

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

解决方法

你遗漏了一些括号:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读