2010-11-13 42 views
1

我想從Linq中的兩個不同的對象中進行選擇以便比較它們。這就是我想,從兩個不同的對象中選擇linq

var myItem = (from abc in firstList.Value 
       from cds in secondList 
       where (abc.Key.theKey == cds.secondList.theSecondKey 
       select cds).SingleOrDefault(); 

雖然我得到一個錯誤:

類型推斷在調用「的SelectMany」

+3

你需要證明你是從選擇對象的結構。 – 2010-11-13 14:14:01

+0

第一個列表是Dictionary >第二個是ClassD的列表..是否足夠好? – 2010-11-13 14:18:24

+0

告訴我們你想用你的收藏實現什麼。我無法理解你的查詢中的一件事,因爲它遠離查詢工作。您是否嘗試根據第一個列表中的項目爲第二個列表選擇一個項目? – Euphoric 2010-11-13 14:25:53

回答

0

失敗如果這是你確切查詢,也可能只是因爲你有無與倫比的括號。試試這個:

var myItem = (from abc in firstList.Value 
       from cds in secondList 
       where abc.Key.theKey == cds.secondList.theSecondKey 
       select cds).SingleOrDefault(); 

不可否認,我可能會重寫使用連接 - 在大多數情況下,連接會更有效率。

但是,如果這是你準確的查詢,請發表一個簡短而這表明問題完整程序。目前還不清楚爲什麼cds例如有secondList屬性。一個演示問題的完整例子會使這個簡單得多。

0

您更具有一開口paranthesis:

var myItem = (from abc in firstList.Value 
       from cds in secondList 
       where abc.Key.theKey == cds.secondList.theSecondKey 
       select cds 
      ).SingleOrDefault();