【发布时间】:2016-06-19 01:40:31
【问题描述】:
假设我有一个{City, State} 的列表。它最初来自数据库,我有 LocationID,但现在我将它加载到内存中。假设我还有一张快餐店表,其中包含城市和州作为记录的一部分。我需要获取与城市和州相匹配的机构列表。
注意:我试图描述一个简化的场景;我的业务领域完全不同。
我想出了以下 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
我觉得一定有更好的东西。对于初学者,我已经在内存中拥有 City/State - 所以返回数据库只是为了进行连接似乎非常低效。我正在寻找某种方式来表达{r.City, r.State} match Any(MyList),其中 MyList 是我收集的城市/州。
更新 我尝试根据以下建议进行更新:
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 编译器不喜欢连接中的匿名类。我明确表示,它停止抱怨。我看看它是否真的在早上有效......
【问题讨论】:
-
听起来
City和State的类型在等号的每一边都不匹配。
标签: c# asp.net entity-framework linq