O'Reilly logo

Code-First Development with Entity Framework by Sergey Barskiy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with related entities

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.

Filtering based on related data

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required