beginner Linq syntax and EF4 question
Question
With the followin开发者_如何学Pythong linq code snip I get a list of clients with address filtered by the specifications but the form of the entities returned is not what i had expected.
The data is 1 client with 2 addresses and 1 client with 1 address.
The query returns 3 rows of clients each with 1 address
- Client 1 => Address1
- Client 1 => Address2
Client 2 => Address3
var query = from t1 in context.Clients.Where(specification.SatisfiedBy()).Include("ClientAddresses") join t2 in context.ClientAddresses.Where(spec.SatisfiedBy()) on t1.ClientKey equals t2.ClientKey select t1;
My expectation was a little more like a list with only two clients in it, one client with a collection of two addresses and one client with a collection of one address.
- Client 1 => Address1 / Address2
- Client 2 => Address3
What am I missing???
Thanks!
did you try something like:
query = query.Distinct();
?
You might need to reveal how the specifications are written to give more data.
For example, I don't see why your query is not something like:
var query = from t1 in context.Clients.Include("ClientAddresses")
where specification.SatisfiedBy() &&
t1.ClientAddresses.Any(spec.SatisfiedBy())
select t1;
Update
See if this works. Not sure how much of that is supported by EF. It's very similar to your oriignal query
var query = (from t1 in context.Clients.Where(specification.SatisfiedBy())
.Include("ClientAddresses")
from t2 in context.ClientAddresses.Where(spec.SatisfiedBy())
where t1.ClientKey == t2.ClientKey
select t1)
.Distinct();
精彩评论