[英]Entity Framework LINQ many to many query
我有很多關系的三張桌子。 這些是:
我正在使用具有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子句中使用什么表達式來執行上述操作? 還是有更好的方法來完成我正在做的事情?
我通常做這種事情的方法是先得到你想要匹配的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);
這取決於你的repostiory實現?!
它應該看起來像這樣:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.