我想交叉2个字典,但我不知道如何存储historicalHashtags[x],我设法只存储了1个字典的键和值。
var intersectedHashTags = currentHashTags.Keys.Intersect(historicalHashTags.Keys).
ToDictionary(x => x, x => currentHashTags[x]);
但是,我希望将结果存储在一个列表中,该列表包括Key、currentHashtags[x]和historicalHashtags[x]
示例:
Dictionary<string, int> currentHashTags =
{ ["#hashtag1", 100], ["#hashtag2", 77], ["#hashtag3", 150], ...}
Dictionary<string, int> historicalHashTags =
{ ["#hashtag1", 144], ["#hashtag4", 66], ["#hashtag5", 150], ...}
List<(string,int,int)> result = { ["#hashtag1", 100, 144] }
2条答案
按热度按时间0s0u357o1#
为了只保留两个字典共有的元素,可以在它们的键上使用
Dictionary
类型的Intersect
方法,然后使用Select
方法将结果转换为IEnumerable<(string, int, int)>
,最后将其转换为列表。x8goxv8g2#
假设您的字典类型为
Dictionary<string, string>
,并且您希望Dictionary<string, List<string>>
作为输出,则如下所示:要获得交集,可以使用简单的连接: