假设我有一个{City, State}
的列表。它最初来自数据库,我有LocationID,但现在我已将它加载到内存中。假设我还有一张快餐店的桌子,其中包含城市和州作为记录的一部分。我需要获得与城市和州相匹配的机构名单。Linq加入两个值
注:我尝试描述一个简化的场景;我的业务领域完全不同。
我想出了以下LINQ的解决方案:
var establishments = from r in restaurants
from l in locations
where l.LocationId == id &&
l.City == r.City &&
l.State == r.State
select r
,我觉得一定有更好的东西。对于初学者来说,我已经在内存中拥有城市/州,所以回到数据库只是为了让连接看起来非常低效。我正在寻找某种方式来说{r.City, r.State} match Any(MyList)
其中MyList是我的城市/州的集合。
UPDATE 我想基于以下建议更新:
List<CityState> myCityStates = ...;
var establishments =
from r in restaurants
join l in myCityStates
on new { r.City, r.State } equals new { l.City, l.State } into gls
select r;
,我得到了以下编译错误: Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
更新2 编译器不喜欢匿名类在加入。我明确表示,并停止抱怨。我会看看它在早上是否真的有效......
这听起来像'城市'和'国家'的类型不匹配在等号的每一边。 – Enigmativity