linq with Include and criteria
Posted
by
JMarsch
on Stack Overflow
See other posts from Stack Overflow
or by JMarsch
Published on 2011-06-24T00:00:55Z
Indexed on
2011/06/24
0:23 UTC
Read the original article
Hit count: 154
LINQ
|entity-framework
How would I translate this into LINQ?
Say I have A parent table (Say, customers), and child (addresses).
I want to return all of the Parents who have addresses in California, and just the california address. (but I want to do it in LINQ and get an object graph of Entity objects)
Here's the old fashioned way:
SELECT c.blah, a.blah
FROM Customer c
INNER JOIN Address a on c.CustomerId = a.CustomerId
where a.State = 'CA'
The problem I'm having with LINQ is that i need an object graph of concrete Entity types (and it can't be lazy loaded.
Here's what I've tried so far:
// this one doesn't filter the addresses -- I get the right customers, but I get all of their addresses, and not just the CA address object.
from c in Customer.Include(c => c.Addresses)
where c.Addresses.Any(a => a.State == "CA")
select c
// this one seems to work, but the Addresses collection on Customers is always null
from c in Customer.Include(c => c.Addresses)
from a in c.Addresses
where a.State == "CA"
select c;
Any ideas?
© Stack Overflow or respective owner