Satyajit
Satyajit

Reputation: 2071

Query inner collection using Entity Framework

I have an object Employee

public class Employee
{
    public int Id {get;set;}
    public ICollection<Address> addresses {get;set;}
}

public class Address
{
    public int Id {get;set;}
    public string AddressLine1 {get;set;}
    public string City {get;set;}
}

Now the Employee is a part of my context.

How do I query the employee object where the address.city is "NY"? I want to traverse to the Address collection from the Employee object in the context.

Thanks

Upvotes: 0

Views: 48

Answers (1)

Marc Cals
Marc Cals

Reputation: 2989

Try with this

MyDBContext.Employee.Where(e => e.Addresses.Any(a => a.city == "NY").ToList();

with this code you will get all Employees, for whom at least one of the address is in New york. If you want that all address of Employee be in New Yowk, then change Any for All.

Upvotes: 1

Related Questions