【问题标题】:Unable to convert IEnumerable<int> to List<T> in Entity Framework无法在实体框架中将 IEnumerable<int> 转换为 List<T>
【发布时间】:2017-05-20 19:04:46
【问题描述】:

我有这样的查询

IEnumerable<int> companies = Employers
                             .Where(p => p.Id == User.ID
                             .SelectMany(p => p.companies)
                             .Select(p => p.Id);


return Employers
       .Where(p => companies.Contains(p.Companies)
       .Include(p => p.FirstName)
       .ToList();      

这里p.Companies指的是

Public Virtual List<Company>

Employers 类下。

出现此错误:

“IEnumerable”不包含“Contains”的定义,最佳扩展方法重载“Queryable.Contains>(IQueryable>,List)”需要“IQueryable>”类型的接收器

【问题讨论】:

  • 搜索网络。这个问题已经被问过很多次了。错误信息非常清楚。您需要将 IQuertable 添加到类对象中。
  • 我是实体框架的新手。我找不到解决方案。请有人帮我找到解决方案。谢谢

标签: c# linq entity-framework-4


【解决方案1】:

这样做:

Where(p => companies.Contains(p.Companies)

您尝试检查companies(来自先前查询的IEnumerable&lt;int&gt;)是否包含一些公司(对象),而不是int 值。

您无法检查 int 值的集合是否包含某个不是 int 的值(Company 是某个对象)。

如果您想查询所有在companies 变量中拥有Id 公司的雇主,请使用此Linq 查询:

return Employers
       .Where(p => p.Companies.Any(c => companies.Contains(c.Id)))
       .Include(p => p.FirstName)
       .ToList();     

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2021-12-02
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多