问题描述
我需要创建使用3个实体框架实体(用户,EnrolmentType和OrganizationEnrolment)创建的对象列表OrganizationEnrolment:
I need to create a list of objects, OrganizationEnrolment, created using 3 Entity Framework entities (User, EnrolmentType and OrganizationEnrolment):
List<OrganizationEnrolment> organizationEnrolments =
context.Organizations
.SelectMany(x => context.Users)
.SelectMany(x => context.EnrolmentTypes)
.Select(y => new OrganizationEnrolment {
EnrolmentType = enrolmentType
Organization = organization,
User = user
})
我的问题是在拥有SelectMany之后如何从3个联接表中获取enrolmentType,组织和用户?请注意以下代码:
My problem is after having the SelectMany how to get the enrolmentType, organization and user from the 3 joined table? Note that the following code:
EnrolmentType = enrolmentType,
Organization = organization,
User = user
不起作用,因为我没有变量enrolmentType,组织和用户.
is not working because I do not have the variables enrolmentType, organization and user.
推荐答案
您可以使用查询语法来获取所有这些变量:
You can use query syntax to get all those variables:
from o in context.Organizations
from u in context.Users
from et in context.EnrolmentTypes
select new OrganizationEnrolment {
EnrolmentType = et
Organization = o,
User = u
}
每个本地范围变量将在select语句中可见.
Each local range variable will be visible in select statement.
Lambda语法(已通过EF6检查):
Lambda syntax (checked with EF6):
context.Organizations.SelectMany(
o => context.Users.SelectMany(
u => context.EnrolmentTypes.Select(
et => new OrganizationEnrolment {
EnrolmentType = et
Organization = o,
User = u
})
)
)
这篇关于SelectMany已应用于3个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!