Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
Questioncompare two dictionary and display difference Pin
mjbaquiran18-Jan-20 0:01
mjbaquiran18-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
mjbaquiran18-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
mjbaquiran18-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
professionalBillWoodruff19-Jan-20 0:19 
AnswerRe: compare two dictionary and display difference Pin
Richard Deeming20-Jan-20 7:52
mveRichard Deeming20-Jan-20 7:52 
It's difficult to answer your question without knowing precisely which types you're using.

Assuming you're using a dictionary of dictionaries:
C#
var dictionary1 = new Dictionary<int, Dictionary<int, int>>
{
    [1] = new Dictionary<int, int>
    {
        [1] = 10,
        [2] = 20,
        [3] = 40,
    },
    [2] = new Dictionary<int, int>
    {
        [1] = 15,
        [2] = 28,
        [3] = 40,
    },
    [3] = new Dictionary<int, int>
    {
        [1] = 15,
        [2] = 28,
        [3] = 40,
    },
};

var dictionary2 = new Dictionary<int, Dictionary<int, int>>
{
    [1] = new Dictionary<int, int>
    {
        [1] = 12,
        [2] = 20,
        [3] = 40,
    },
    [2] = new Dictionary<int, int>
    {
        [1] = 15,
        [2] = 25,
        [3] = 40,
    },
    [3] = new Dictionary<int, int>
    {
        [1] = 15,
        [2] = 28,
        [3] = 40,
    },
};
and you want to keep the values from dictionary2, then something like this should work:
C#
var result = new Dictionary<int, Dictionary<int, int>>();
foreach (var pair in dictionary2)
{
    if (!dictionary1.TryGetValue(pair.Key, out var value))
    {
        result.Add(pair.Key, pair.Value);
    }
    else
    {
        var differences = new Dictionary<int, int>();
        foreach (var item in pair.Value)
        {
            if (!value.TryGetValue(item.Key, out var i) || item.Value != i)
            {
                differences.Add(item.Key, item.Value);
            }
        }
        if (differences.Count != 0)
        {
            result.Add(pair.Key, differences);
        }
    }
}
NB: Items in the first dictionary (or one of its nested dictionaries) which are not in the second dictionary will be ignored.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: compare two dictionary and display difference Pin
mjbaquiran20-Jan-20 19:53
mjbaquiran20-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
professionalF-ES Sitecore14-Jan-20 22:49 
SuggestionRe: Exception Handling Question Pin
Richard Deeming15-Jan-20 0:38
mveRichard Deeming15-Jan-20 0:38 
AnswerRe: Exception Handling Question Pin
Gerry Schmitz15-Jan-20 13:44
mveGerry Schmitz15-Jan-20 13:44 

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.