包含和标准的linq

包含和标准的linq,linq,entity-framework,Linq,Entity Framework,我如何将其转换为LINQ 假设我有一个父表(比如客户)和子表(地址)。 我想返回所有在加州有地址的父母,只返回加州地址。(但我想在LINQ中执行此操作,并获得实体对象的对象图) 这是一种老式的方式: SELECT c.blah, a.blah FROM Customer c INNER JOIN Address a on c.CustomerId = a.CustomerId where a.State = 'CA' 我使用LINQ的问题是,我需要一个具体实体类型的对象图(并且不能延迟加载)

我如何将其转换为LINQ

假设我有一个父表(比如客户)和子表(地址)。
我想返回所有在加州有地址的父母,只返回加州地址。(但我想在LINQ中执行此操作,并获得实体对象的对象图)

这是一种老式的方式:


SELECT c.blah, a.blah
FROM Customer c
INNER JOIN Address a on c.CustomerId = a.CustomerId
where a.State = 'CA'
我使用LINQ的问题是,我需要一个具体实体类型的对象图(并且不能延迟加载)

以下是我迄今为止所尝试的:

编辑:根据请求添加上下文实例化


// 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.

var ctx = new CustomersContext() // dbContext -- using EF 4.1 

from c in ctx.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
var ctx = new CustomersContext() // dbContext -- using EF 4.1 
from c in ctx.Customer.Include(c => c.Addresses)
from a in c.Addresses
where a.State == "CA"
select c;

有什么想法吗?

根据上面的代码,您似乎已经在
Customer
变量中重新水化了客户对象的集合

当您填写上面的
客户
集合时,您需要调用
包含
函数。这样,框架将在从数据上下文检索时重新水化包含的对象


但是,实际的查询代码看起来不错。

很抱歉,您能提供一个代码示例吗?我尝试将第二个查询包装成parens,然后在结果中调用include。这返回了我的客户和地址,但是--我得到了所选客户的所有地址。我只想要加利福尼亚地址。根据@reddog的回答,pl请为客户添加声明和实例化代码,如果您以前加载过它,结果将不同于仅引用context@BenLaan:抱歉--我从LINQPad中提取了代码--我将添加实例化,但之前没有加载--查询是从无状态web服务运行的。