如何比较两个词典 [英] How to compare two dictionaries
本文介绍了如何比较两个词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一本词典使用
I have a dictionary Used
Dim Used As New Dictionary(Of Integer, Boolean)
111,true
5,true
125,true
我有第二本字典ByteDic
I have second dictionary ByteDic
Dim ByteDic As Dictionary(Of byte, Integer)
111,1233
22,334
125,123
10, 128
我想选择ByteDic的最大值,该密钥不包含二手(我想从ByteDic中选择334值)
我尝试过:
I want select max value of ByteDic that key not contain by Used (I want select 334 value from ByteDic)
What I have tried:
int MaxValue = ByteDic.Select(x => Used.Keys.ToList.IndexOf(x.Key) > -1).Max;
Dim MaxValue As Integer = ByteDic.Select(Function(x) Used.Keys.ToList.IndexOf(x.Key) > -1).Max
推荐答案
通过在比较中使用Select,您将只获得bool结果。你需要在Where
语句中使用比较。
另外,使用ToList
在Linq查询中,ToList
将被称为 ,用于每次 比较效率低下。因此,您最好使用包含列表的临时变量,以便只调用一次。
考虑到这一点,我建议使用类似的东西:
By using Select on a comparison, you will only get a bool result. You need to use the comparison in aWhere
statement.
Also, usingToList
inside a Linq query, theToList
will be called for each comparison which is highly inefficient. So you wouild be better off using a temporary variable that contains the list so that it is only called once.
With that in mind, I would suggest using something like:
Dim result = ByteDic.Where(Function(x) Not used.Any(Function(y) y.Key.Equals(x.Key))).Max(Function(x) x.Value)
或
or
Dim result = ByteDic.Where(Function(x) Not used.Keys.Contains(x.Key)).Max(Function(x) x.Value)
或
or
Dim result = ByteDic.Where(Function(x) Not used.ContainsKey(x.Key)).Max(Function(x) x.Value)
这篇关于如何比较两个词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文