How to return all records and whether a related record exists?
Posted
by
David Glenn
on Stack Overflow
See other posts from Stack Overflow
or by David Glenn
Published on 2010-12-23T10:45:33Z
Indexed on
2010/12/23
10:54 UTC
Read the original article
Hit count: 297
entity-framework
|linq-to-entities
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
© Stack Overflow or respective owner