Linq -通过字典连接KeyValuePair.Value本身就是一个集合

本文关键字:集合 一个 字典 -通 连接 KeyValuePair Value Linq | 更新日期: 2023-09-27 18:02:26

试着弄清楚在下面的例子中连接应该是什么样子。任何帮助确定是否有可能为以下内容编写linq将非常感谢!

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

我希望从字典中选择col1中的项存在于字典的Value List集合中的所有键。

        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是一个List而不是一个字符串,等于c1 -有指针吗?

Linq -通过字典连接KeyValuePair.Value本身就是一个集合

我不知道为什么需要连接…如果你在"foo"上选择,那么下面的命令将产生相同的结果:

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

或者使用join

        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;
from kvp in dict
from e in kvp.Value
... // join on e here
var result = from c1 in col1.Keys
             from c2 in col1.Values
             where c1 == c2
             select c1;