Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two different dictionaries of different size, can we compare both these and extract the values which are not there in dictionary 1?

What I have tried:

I have two different dictionaries of different size, can we compare both these and extract the values which are not there in dictionary 1?
Posted
Updated 13-Jul-18 22:47pm

1 solution

Use the Linq Except method:
C#
Dictionary<string, string> d1 = new Dictionary<string, string>();
d1.Add("Joe", "2, Barfield Way");
d1.Add("Mike", "17, Apollo Avenue");
d1.Add("Jane", "69, Lance Drive");
Dictionary<string, string> d2 = new Dictionary<string, string>();
d2.Add("Joe", "2, Barfield Way");
d2.Add("Jane", "69, Lance Drive");
var diff = d1.Except(d2);
 
Share this answer
 
Comments
Member 13911288 14-Jul-18 4:55am    
Thanks for the answer, my dictionaries have time in HH:mm format. Dictionary 1 has time from 00:00 to 23:45 and dictionary two has random hours like 00:15, 00:45, 15:00. Here's my dictionary in brief

Dictionary1
(startdttm1,"00:00")
(enddttm1,"00:15")
(startdttm2,"00:15")
(enddttm2,"00:30")
(startdttm3,"00:30")
(enddttm4,"00:45")
......
(startdttmn,"23:30")
(enddttmn,"23:45")

now my second dictionary has booking in random order

(startdttm1,"00:00")
(enddttm1,"00:30")
(startdttm2,"02:00")
(enddttm1,"02:30")

so now how to compare dictionary 1 to dictionary 2 and I need to pick values from dictionary 1 which are greater in values than dictionary 2? I am able to DateTime.Parse and compare two times but the thing is if I am failing to retrieve the exact values from dictionary 1 which are greater than all the values in dictionary 2.

Dictionary 1 is large and Dictionary 2 is small.
OriginalGriff 14-Jul-18 5:10am    
First off, change your storage.
Never keep data in strings if there is a more appropriate datatype - which in this case is Timespan, which has comparison operators.
Storing the start and end times like that is also a bad idea, as all comparisons will be string based - and they are done by character by character comparison, with the whole result depending on the result of the first difference. That menas that any idea of sort order goes out the window.
Second, don't use a dictionary.
DIctionaries are not sorted: they are hashed so they do not preserve the order in which you insert values.

I think you have some major problems here, and no amount of patching is going to solve them in a "good way" - you need to go back to the start of your data collection, and improve the whole storage process - it's fatally flawed at the moment and will only continue to give you more and more problems until you sort it properly.
Member 13911288 14-Jul-18 5:14am    
so what do you suggest in this case where I need to compare two collections of datetime values?
OriginalGriff 14-Jul-18 5:26am    
I can't suggest anything - I have no idea what your homework is asking you to do, or what the rest of your code looks like.
I have no idea why you are storing what, or where you get it from - only you know that!

And as I said - your whole data storage is the problem and needs to be revised. Get that right, and the comparison either becomes obvious or possibly unnecessary - either way, there is no point in trying to fix a problem here if the whole basis of the rest of your code is going to have to change.
Member 13911288 14-Jul-18 5:29am    
What if I convert the two dictionaries into List of Values and compare both and extract only the values which are greater than list 2? Can you suggest anything on this?

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