[英]Entity Framework ICollection to List
I am trying to do something like this: 我正在尝试做这样的事情:
List<Department> t = db.Employees
.Where(e => e.CompanyId.Equals(12345))
.Where(e => e.Departments.Any(d => d.IsActive.Equals(true)))
.Select(e => e.Departments);
But it gives me an error telling me that can't implicitly convert from IQueryable<ICollection<Department>> to List<Department>
. 但这给了我一个错误,告诉我不能从
IQueryable<ICollection<Department>> to List<Department>
隐式转换IQueryable<ICollection<Department>> to List<Department>
。
My Linq is a bit rusty. 我的Linq有点生锈。 What is the right way to bring this down to the
List<Department>
将其归纳到
List<Department>
的正确方法是什么
You have a queryable collection of a collection of departments (note the plural e.Departments
). 您有一个可查询的部门集合(请注意复数
e.Departments
)。 As a result, you can use .SelectMany()
to flatten your collection and then resolve the IQueryable with .ToList()
. 其结果是,你可以使用
.SelectMany()
扁平化的集合,然后解决与IQueryable的.ToList()
List<Department> t = db.Employees
.Where(e => e.CompanyId.Equals(12345))
.Where(e => e.Departments.Any(d => d.IsActive.Equals(true)))
.SelectMany(e => e.Departments).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.