In the query below, I want to return people that meet multiple conditions. Some of the conditions apply to fields in the table containing the people to be returned. The other condition is applied to a different table (EmailAddresses) linked to the main table (People) via PersonId.
var t = People.Where(x =>
开发者_JAVA技巧 x.Type == 102 &&
x.FirstName == "Bob" &&
x.LastName == "Williams" &&
x.EmailAddresses.Where (ea=> ea.EmailAddress
== "bob.williams@acme.org")
)
.Select(x => x.PersonId)
How do I do this?
Do understand this right, at least one of them should have that adress? If yes, use the Any
method:
var t = People.Where(x =>
x.Type == 102 &&
x.FirstName == "Bob" &&
x.LastName == "Williams" &&
x.EmailAddresses.Any(ea=> ea.EmailAddress
== "bob.williams@acme.org")
)
.Select(x => x.PersonId)
Any
returns true if at least one of the elements of the IQueryable<T>
fulfills the predicate.
I think you could do it with a join, something like this...
var t = from p in People
join e in EmailAddress on p.PersonId equals e.PersonId into pe
from a in pe.DefaultIfEmpty
where p.Type == 102 &&
p.FirstName == "Bob" &&
p.LastName == "Williams" &&
a.EmailAddress == "bob.williams@acme.org"
select p.PersonId
精彩评论