无法将 IEnumerable<int> 转换为 Entity Framework 中的 List<T>

Unable to convert IEnumerable<int> to List<T> in Entity Framework

我有这样的查询

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>

下classEmployers

发生此错误:

'IEnumerable' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains>(IQueryable>,List)' requires a receiver of type 'IQueryable>'

这样做:

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

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

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

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

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