2011-07-19 17 views
0

試圖讓我的腦袋圍繞下面的示例應該看起來像什麼樣的聯接。任何幫助確定是否有可能寫linq爲以下將大大apprieciated!Linq - 通過字典加入KeyValuePair.Value是一個集合本身

 List<string> col1; 
     Dictionary<string, List<string>> join2; 

我希望從字典中選擇字典中值列表集合中col1項中存在的所有項。

 IEnumerable<string> query = from c1 in col1 
            join kvp in join2 on c1 equals kvp.Value 
            where c1 == "foo" 
            orderby kvp.Key 
            select kvp.Key; 

顯然上述未按kvp.Value是一個列表,而不是等同於C1字符串 - 任何指針?

+0

其中* col1中的所有項目都顯示在值中?哪裏* col1中的任何項目出現在值中?訂單重要嗎? 「富」在哪裏進來?目前尚不清楚你想要實現的目標。請提供一些示例數據。 –

+0

嘿喬恩,我想鏈接一個KeyValuePair的Value屬性集合來返回一個Key。條件'foo'部分表示我希望在值集合中找到的值以返回密鑰。我在_Any_匹配之後 - 所以使用一個contains的子句和where條件符合我的要求。感謝您的期待。 –

+0

使用任何電話也可以工作... –

回答

0
from kvp in dict 
from e in kvp.Value 
... // join on e here 
0
var result = from c1 in col1.Keys 
      from c2 in col1.Values 
      where c1 == c2 
      select c1; 
1

我不知道爲什麼加入需要...如果你是「富」,然後選擇以下會產生相同的結果:

 IEnumerable<string> query = from kvp in join2 
           where kvp.Value.Contains ("foo") 
           orderby kvp.Key 
           select kvp.Key; 

或者與加盟

 IEnumerable<string> query = from c1 in col1 where c1 == "foo" from kvp in join2 
           where kvp.Value.Contains (c1) 
           orderby kvp.Key 
           select kvp.Key; 
+0

謝謝 - 這正是我想要的 - 這是一個簡單的例子,真正的複雜原因使我的商業案例更有意義。沒有意識到多個froms是可能的,現在是完全合理的。謝謝!!! –