Click here to Skip to main content
14,489,204 members
   

C#

 
GeneralRe: Clear comments and rename var/method names at compile time Pin
Gerry Schmitz18-Jan-20 17:30
mveGerry Schmitz18-Jan-20 17:30 
Questioncompare two dictionary and display difference Pin
mjbaquiran18-Jan-20 0:01
Membermjbaquiran18-Jan-20 0:01 
QuestionRe: compare two dictionay and display difference Pin
Eddy Vluggen18-Jan-20 0:30
professionalEddy Vluggen18-Jan-20 0:30 
AnswerRe: compare two dictionay and display difference Pin
mjbaquiran18-Jan-20 7:05
Membermjbaquiran18-Jan-20 7:05 
AnswerRe: compare two dictionay and display difference Pin
Gerry Schmitz18-Jan-20 6:40
mveGerry Schmitz18-Jan-20 6:40 
GeneralRe: compare two dictionay and display difference Pin
mjbaquiran18-Jan-20 7:08
Membermjbaquiran18-Jan-20 7:08 
GeneralRe: compare two dictionay and display difference Pin
Gerry Schmitz18-Jan-20 7:52
mveGerry Schmitz18-Jan-20 7:52 
AnswerRe: compare two dictionary and display difference Pin
BillWoodruff19-Jan-20 0:19
mveBillWoodruff19-Jan-20 0:19 
You need to specify the result you want in more detail.

0. if each dictionary has an identical key, but the key's values are different, what do you want to do ?

     a. keep the value from one of the two dictionaries ? which one ?

     b. execute some comparison function you define ?

1. do you assume Key and Value are the same Types ?

2. assuming #1: what if one dictionary has a KVPair with a key 'foo, and the other dictionary has a KVPair with a value of 'foo ?

See if this gives you some ideas:
private Dictionary<int, int> d1 = new Dictionary<int, int> {{1, 10}, {2, 20}, {3, 40}};

private Dictionary<int, int> d2 = new Dictionary<int, int> {{1, 12}, {2, 20}, {5, 40}, {6, 50}};

private Dictionary<T1,T2> GetDictDiff<T1, T2>(Dictionary<T1, T2> d1, Dictionary<T1, T2> d2)
{
    Dictionary<T1,T2> kvpDifferences = new Dictionary<T1, T2>();
    
    foreach (KeyValuePair<T1, T2> kvp in d1)
    {
        if (! d2.Keys.Contains(kvp.Key))
        {
            // d1 key not in d2
            kvpDifferences.Add(kvp.Key, d1[kvp.Key]);
        }
        else
        {
            // d1 key in d2
            if (! Equals(d2[kvp.Key], d1[kvp.Key]))
            {
                // different values
                kvpDifferences.Add(kvp.Key, d1[kvp.Key]);
            }
        }
    }

    foreach (KeyValuePair<T1, T2> kvp in d2)
    {
        if (! d1.Keys.Contains(kvp.Key))
        {
            // d2 key not in d1
            kvpDifferences.Add(kvp.Key, d2[kvp.Key]);
        }
    }

    return kvpDifferences;
}

// usage in some method: var test = GetDictDiff<int,int>(d1, d2);

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 20-Jan-20 3:28am.

AnswerRe: compare two dictionary and display difference Pin
Richard Deeming20-Jan-20 7:52
mveRichard Deeming20-Jan-20 7:52 
GeneralRe: compare two dictionary and display difference Pin
mjbaquiran20-Jan-20 19:53
Membermjbaquiran20-Jan-20 19:53 
GeneralRe: compare two dictionary and display difference Pin
Richard Deeming21-Jan-20 1:19
mveRichard Deeming21-Jan-20 1:19 
QuestionOleDb Error Pin
Kevin Marois15-Jan-20 8:57
professionalKevin Marois15-Jan-20 8:57 
AnswerRe: OleDb Error Pin
OriginalGriff15-Jan-20 9:03
mveOriginalGriff15-Jan-20 9:03 
GeneralRe: OleDb Error [UPDATED] Pin
Kevin Marois15-Jan-20 9:34
professionalKevin Marois15-Jan-20 9:34 
GeneralRe: OleDb Error Pin
OriginalGriff15-Jan-20 9:41
mveOriginalGriff15-Jan-20 9:41 
GeneralRe: OleDb Error Pin
Kevin Marois15-Jan-20 10:57
professionalKevin Marois15-Jan-20 10:57 
GeneralRe: OleDb Error [UPDATED] Pin
Dave Kreskowiak15-Jan-20 9:45
mveDave Kreskowiak15-Jan-20 9:45 
GeneralRe: OleDb Error [UPDATED] Pin
Kevin Marois15-Jan-20 10:51
professionalKevin Marois15-Jan-20 10:51 
GeneralRe: OleDb Error [UPDATED] Pin
Eddy Vluggen15-Jan-20 12:15
professionalEddy Vluggen15-Jan-20 12:15 
GeneralRe: OleDb Error [UPDATED] Pin
Dave Kreskowiak15-Jan-20 12:50
mveDave Kreskowiak15-Jan-20 12:50 
GeneralRe: OleDb Error [UPDATED] Pin
Eddy Vluggen15-Jan-20 12:57
professionalEddy Vluggen15-Jan-20 12:57 
QuestionException Handling Question Pin
Kevin Marois14-Jan-20 18:53
professionalKevin Marois14-Jan-20 18:53 
AnswerRe: Exception Handling Question Pin
OriginalGriff14-Jan-20 21:19
mveOriginalGriff14-Jan-20 21:19 
AnswerRe: Exception Handling Question Pin
F-ES Sitecore14-Jan-20 22:49
mveF-ES Sitecore14-Jan-20 22:49 
SuggestionRe: Exception Handling Question Pin
Richard Deeming15-Jan-20 0:38
mveRichard Deeming15-Jan-20 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.