Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dictionary Used
Dim Used As New Dictionary(Of Integer, Boolean)
111,true
5,true
125,true

I have second dictionary ByteDic
Dim ByteDic As Dictionary(Of byte, Integer)
111,1233
22,334
125,123
10, 128

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
Posted
Updated 7-Jul-18 19:33pm
v4

1 solution

By using Select on a comparison, you will only get a bool result. You need to use the comparison in a Where statement.

Also, using ToList inside a Linq query, the ToList 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:
VB
Dim result = ByteDic.Where(Function(x) Not used.Any(Function(y) y.Key.Equals(x.Key))).Max(Function(x) x.Value)

or
VB
Dim result = ByteDic.Where(Function(x) Not used.Keys.Contains(x.Key)).Max(Function(x) x.Value)

or
VB
Dim result = ByteDic.Where(Function(x) Not used.ContainsKey(x.Key)).Max(Function(x) x.Value)
 
Share this answer
 
Comments
gacar 8-Jul-18 12:54pm    
Thank you very much Graeme_Grant for the solution and the explanatory information. Other expressions worked except the first.
Graeme_Grant 8-Jul-18 15:43pm    
Glad that it helped.... the first one should have worked however was untested off the top of my head.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900