Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字典的键与另一个字典的值进行比较_C#_Linq_Dictionary - Fatal编程技术网

C# 将字典的键与另一个字典的值进行比较

C# 将字典的键与另一个字典的值进行比较,c#,linq,dictionary,C#,Linq,Dictionary,我有两本字典,都是你写的Dictionary类型: 我想从字典1中选择匹配的条目范围 字典2中的值/条目 从Edit2中的输出来看,您似乎想要从Dictionary 2中获取值。你不能用钥匙做任何事。每个值都是一个列表。在您的示例中,键为1的值的第一个列表中的所有字符串在字典1中都有相应的键。显然,这是决定输出中是否存在完整值的条件 键为2的值的第一个列表中有一个元素不是字典1中的键。因此,输出中没有任何值 不清楚:如果第二个列表匹配而不是第一个列表,该怎么办 Key Value 3

我有两本字典,都是你写的
Dictionary类型:

我想从字典1中选择匹配的条目范围 字典2中的值/条目

从Edit2中的输出来看,您似乎想要从Dictionary 2中获取值。你不能用钥匙做任何事。每个值都是一个
列表
。在您的示例中,键为1的值的第一个列表中的所有字符串在字典1中都有相应的键。显然,这是决定输出中是否存在完整值的条件

键为2的值的第一个列表中有一个元素不是字典1中的键。因此,输出中没有任何值

不清楚:如果第二个列表匹配而不是第一个列表,该怎么办

Key     Value
3       "foo", "bar"
        "01", "01.1"
这也应该出现在你的最终结果中吗

不清楚您想要一个
列表
,还是想要一个包含所有匹配值的大
列表
?重复值呢

假设您只想检查列表列表中的第一个列表:

我们只看字典2中的值,键被丢弃。然后,从这个值集合中的每个列表中,我们取第一个(如果有),并作为一个单独的属性记住完整的列表

当然,空列表不应出现在最终结果中,因此我们只保留具有第一个元素的列表:

// Only use the values of dictionary 2:
IEnumerable<List<List<string>>> dict2Values = dictionary2.Values

// then for easy comparison extract the first list
var separatedFirstList = dict2Values.Select(listOfLists=> new
{
     FirstList = listOfLists.FirstOrDefault(), // this is a List<string> or null
     AllLists = listOfLists,    // original List<List<string>> where FirstList is the first
})

// keep only the elements that have a first list:
.Where(stringListWithFirstElement => stringListWithFirstElement.FirstList != null);
从这个序列中,我们只想保留那些
FirstString
中的所有元素都是
字典1
的键的元素:

IEnumerable<string> keysDictionary1 = dictionary1.Keys;
var matchingItems = separatedFirstList
    .Where(item => item.FirstList.All(txt => keysDictionary1.Contains(txt));
带有
FirstString={“02”,“02.21”}
的元素将被删除,因为不是字典1中的键所在的FirstString的所有元素

最后:去掉第一个字符串:

List<List<String>> finalResult = matchingItems
    .Select(matchingItem => matchingItem.FullList);
List finalResult=matchingItems
.选择(matchingItem=>matchingItem.FullList);
或者如果希望结果是一个
列表

List finalResult=matchingItems.SelectMany(matchingItem=>matchingItem.FullList);

请考虑创建一个大的LINQ语句。由于中间步骤使用延迟执行,我不确定这是否会提高性能。但是,我确信它会降低可读性。

似乎您正在寻找使用linq的
加入

var result = from d1 in dict1
             join d2 in dict2
             on double.Parse(d1.Key) equals double.Parse(d2.Key)
             select d2.Value;

在上面的查询中,我们通过键的相等性(通过将键解析为数字)连接两个字典,对于每个匹配,我们从第二个字典中选择
值作为匹配的结果。

这将为您提供您想要的:

var result = dict2
            .Select(kvp => kvp.Value)
            .Where(list => list.Where(l => l.Join(dict1.Keys, s1 => s1, s2 => s2, (s1, s2) => s1).Count() == l.Count).Count() > 0)
            .ToList();

为什么dict1中键
02
的条目没有显示在结果中?此键与dict2!中键
2
的值匹配!?老实说:我宁愿为您的结构构建专用类,并实现所需的函数和属性。这通常比摆弄嵌套的字典和列表更快、更容易处理。@arekzyla:在OP中添加。使用FooBar示例从来不是一个好主意,因为它们可能意味着任何东西。我还不清楚你想要什么。我的意思是,从你的例子来看,它所要做的就是返回Dict2中key=1的值吗?@Allix试试这个。它适用于您在OP中提到的值。您可以将其与您可能遇到的其他场景一起检查。让我知道它是否有效
string selectedId=Convert.ToString(1);var result=dict2.Where(d2=>d2.Key==selectedId).Where(d2=>d2.Value.Any(d2lst=>d2lst.Any(d2lst1=>dict1.Keys.Contains(d2lst1)).Select(d2=>d2.Value.ToList()
// Only use the values of dictionary 2:
IEnumerable<List<List<string>>> dict2Values = dictionary2.Values

// then for easy comparison extract the first list
var separatedFirstList = dict2Values.Select(listOfLists=> new
{
     FirstList = listOfLists.FirstOrDefault(), // this is a List<string> or null
     AllLists = listOfLists,    // original List<List<string>> where FirstList is the first
})

// keep only the elements that have a first list:
.Where(stringListWithFirstElement => stringListWithFirstElement.FirstList != null);
{
    FirstString = {"01", "01.1"},
    FullList =    {"01", "01.1"}, {"foo", "bar"}, {...}, {...},
},
{
    FirstString = {"02", "02.21"},
    FullList =    {"02", "02.21"}, {"value1" "value2"}, ...
},
{
     FirstString = ...,
     FullList = ...,
},
...
IEnumerable<string> keysDictionary1 = dictionary1.Keys;
var matchingItems = separatedFirstList
    .Where(item => item.FirstList.All(txt => keysDictionary1.Contains(txt));
{
    FirstString = {"01", "01.1"},
    FullList =    {"01", "01.1"}, {"foo", "bar"}, {...}, {...},
},
...
List<List<String>> finalResult = matchingItems
    .Select(matchingItem => matchingItem.FullList);
List<string> finalResult = matchingItems.SelectMany(matchingItem => matchingItem.FullList);
var result = from d1 in dict1
             join d2 in dict2
             on double.Parse(d1.Key) equals double.Parse(d2.Key)
             select d2.Value;
var result = dict2
            .Select(kvp => kvp.Value)
            .Where(list => list.Where(l => l.Join(dict1.Keys, s1 => s1, s2 => s2, (s1, s2) => s1).Count() == l.Count).Count() > 0)
            .ToList();