我有很多关系的三张桌子。这些是:
我正在使用具有IQueryable GetItems()的Entity Framework和Repository模型。如何检索给定区域id(t033)的所有地理编码(t031)条目?我实际上想说:
从t031_geocode中选择*,其中t031_id in(从t032_region_to_geocode_mapping中选择t031_id,其中t033_id = @RegionId)
但是我怎么说这个使用实体框架和LINQ?我想它始于:
var data = _repository.GetItems<t031_geo_code>().Where(g => ???);
但在Where子句中有什么表达式来做上面的事情?或者有更好的方法来完成我正在做的事情吗?
答案 0 :(得分:4)
我通常会这样做的方法是首先获得要匹配的ID列表,然后获取与这些ID匹配的记录子集,例如:
var ids = _repository.Get<t032_region_to_geocode_mapping>().Where(x => x.t33_id = @RegionId).Select(x => x.t03_id).ToList();
var data = _repository.Get<t031_geocode>().Where(x => ids.Contains(x.t031_id);
答案 1 :(得分:0)
这取决于您的存储库实现?!
这应该是这样的:
var regionKeyOrKeys = new int []{XX};
var codes = (from region in t031_geocode.GetAllItems..// Get all items no problem here! because of
where (regionKeyOrKeys.Contains(region.Key))
select region).ToList();