【发布时间】:2015-08-20 15:48:50
【问题描述】:
当我访问所有城市时,我的代码是这样的。
public IQueryable<City> GetAll()
{
var result = from s in this.Context.Cities.Include("States.Countries") select s;
return result;
}
这工作正常,包括州和县。我想通过国家 ID 获取城市,下面是我的代码。在下面的代码中,我想包含每个城市的 States.Countires。我该怎么做?
public IEnumerable<City> GetByCountriesId(int Id)
{
var result = from s in this.Context.Countries
join a in this.Context.States on s.Id equals a.Country_Id
join b in this.Context.Cities on a.Id equals b.States_Id
where s.Id == Id
select b;
return result;
}
【问题讨论】:
-
如果
City和State具有适当的导航属性,this.Context.Cities.Include("State.Country").Where(c=>c.State.Country.Id==Id)就足够了。 -
您使用的是什么版本的 EF?我问是因为您使用的带有字符串参数的 Include 较旧,您应该使用表达式方法来支持编译时。例如:'.Include(x => x.States)'
标签: c# linq entity-framework linq-to-entities