我需要创建一个对象列表,OrganizationRegistration,使用3个实体框架实体(用户、EnrolmentType和OrganizationRegistration)创建:

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个joined表中获取enrollmenttype、organization和user?注意以下代码:
EnrolmentType = enrolmentType,
Organization = organization,
User = user

无法工作,因为我没有enrolmenttype、organization和user变量。

最佳答案

可以使用查询语法获取所有这些变量:

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语句中可见。
lambda语法(用ef6检查):
context.Organizations.SelectMany(
          o => context.Users.SelectMany(
              u => context.EnrolmentTypes.Select(
                  et => new OrganizationEnrolment {
                       EnrolmentType = et
                       Organization = o,
                       User = u
                  })
              )
         )

10-08 01:47