Beginner Question on traversing a EF4 model
- by user564577
I have a basic EF4 model with two entities. I'm using Self Tracking Entities
Client has ClientAddresses relationship on clientkey = clientkey. (1 to many)
How do i get a list/collection of client entities (STE) and their addresses (STE) but only ones where they live in a particular state or some such filter on the address???
This seems to filter and bring back clients but doesnt bring back addresses.
var j = from client in context.Clients
where client.ClientAddresses.All(c => c.ZIP == "80923")
select client;
I cant get this to create the Addresses because ClientAddresses is IEnumerable and it needs a TrackableCollection
var query = from t1 in context.Clients
join t2 in context.ClientAddresses
on t1.ClientKey equals t2.ClientKey
where t2.ZIP == "80923"
select new Client
{
FirstName = t1.FirstName,
LastName = t1.LastName,
IsEnabled = t1.IsEnabled,
ClientKey = t1.ClientKey,
ChangeUser = t1.ChangeUser,
ChangeDate = t1.ChangeDate,
ClientAddresses = from a in t1.ClientAddresses
select new ClientAddress
{
AddressKey = a.AddressKey,
AddressLine1 = a.AddressLine1,
AddressLine2 = a.AddressLine2,
AddressTypeCode = a.AddressTypeCode,
City = a.City,
ClientKey = a.ClientKey,
State = a.State,
ZIP = a.ZIP
}
};
Any pointers would be appreciated.
Thanks
Edit:
This seems to work....
var j = from client in context.Clients.Include("ClientAddresses")
where client.ClientAddresses.Any(c => c.ZIP == "80923")
select client;