根据我的阅读,您的查询仅在具有至少一个活动地址的情况下才获得人 1234。但是,它将包括与人员 1234 相关的所有地址。例如,如果人员 1234 有一个活动地址和五个非活动地址,您将获得人员 1234 的详细信息,以及它的六个总地址,而不仅仅是活动地址。
出于您的目的,我认为子查询可以。
var person = repositoryProvider.Repository.GetQuery<Person>()
.Where (a => a.PersonID ==1234)
.Include(a => a.Address)
.Include(a => a.Employer)
.Select (p => new Person { Id = p.Id, // <-- set the person's properties that you need using the person details that you have extracted.
FirstName = p.FirstName,
Employer = p.Employer.ToList()
Addresses = p.Addresses.Where(a => a.IsActive == true) // <-- transform the resulting entity such that it will only return active addresses
}).FirstOrDefault();
另一种可能更容易阅读的方法是将其分为两部分。第一部分将提取人员详细信息:
var person = repositoryProvider.Repository.GetQuery<Person>()
.Where (a => a.PersonID ==1234)
.Include(a => a.Addresses)
.Include(a => a.Employer)
.Select (p => p);
然后第二部分是将人的地址限制为仅活动地址:
person.Addresses = person.Addresses.Where (a => a.IsActive == true).ToList();