At times, we need to write queries that involve more than a single entity type. We are now going to take a look at how we can work with entities that are involved in relationships.
Sometimes, we need to filter based on related data. For example, we want to find people who have phone numbers that start with "1"
. It is worth noting that this additional task is performed purely based on relationships between a person and phone entities, using the Phones
property on the person
class, as shown in the following code snippet:
var query = from person in context.People where person.IsActive && person.Phones.Any(ph => ph.PhoneNumber.StartsWith("1")) select person; var methodQuery = context.People .Where(p => p.IsActive && p.Phones.Any(ph => ph.PhoneNumber.StartsWith("1")));
Again, we use the plain string function, StartsWith
in the preceding case, yet this will translate...