假設我有一個{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