Beginner Question on traversing a EF4 model

Posted by user564577 on Stack Overflow See other posts from Stack Overflow or by user564577
Published on 2011-01-05T21:34:28Z Indexed on 2011/01/06 2:54 UTC
Read the original article Hit count: 227

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;

© Stack Overflow or respective owner

Related posts about entity-framework-4

Related posts about linq-to-entities