开发者

Linq - Joining through a dictionary where the KeyValuePair.Value is a collection itself

开发者 https://www.devze.com 2023-03-21 02:36 出处:网络
trying to get my head round what the joins should look like for the example below. Any help determining if it is poss开发者_开发技巧ible to write linq for the following would be greatly apprieciated!

trying to get my head round what the joins should look like for the example below. Any help determining if it is poss开发者_开发技巧ible to write linq for the following would be greatly apprieciated!

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

I wish to select all keys from the dictionary where the items in col1 exist in the Value List collection of the dictionary.

        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;

Obviously the above fails as kvp.Value is a List and not a string to equate to c1 - any pointers?


I am not sure why the join is needed... if you are select on "foo" then the following would yield the same result:

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

Alternatively with the 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号