How to return all records and whether a related record exists?
- by David Glenn
Using Entity Framework 4 CTP5 I have a basic model and a basic DbContext that works
public class Customer {
public int CustomerId { get; set; }
public int Name { get; set; }
//...
public ICollection<Address> Addresses { get; set; }
public bool HasAddress {
get {
return Addresses.Count > 0;
}
}
}
public class Address {
public int AddressId { get; set; }
public string StreetLine1 { get; set; }
//....
public Customer Customer { get; set; }
}
How can I query my DbContext to return all customers and whether they have an address?
A customer can have multiple addresses and I don't want to return all the addresses for each customer when I am only interested in whether they have an address or not. I use
context.Customers.Include(c => c.Addresses) but that returns all addresses for each customer