Click here to Skip to main content
15,880,469 members
Home / Discussions / C#
   

C#

 
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 
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 
If you're storing the value as a string in that format, then you're going to need to write custom code to parse that format to extract the keys and values from it.

For example:
C#
private static readonly Regex ItemPattern = new Regex(@"^\s*{\s*(?<key>[^,]+)\s*,\s*(?<value>[^;]+)(\s*;\s*(?<key>[^,]+)\s*,\s*(?<value>[^;]+))*\s*}\s*$", RegexOptions.ExplicitCapture);

private static Dictionary<string, string> ParseItem(string value)
{
    var match = ItemPattern.Match(value);
    if (!match.Success) throw new ArgumentException($"Item '{value}' is not in the correct format.", nameof(value));

    var keys = match.Groups["key"].Captures.Cast<Capture>();
    var values = match.Groups["value"].Captures.Cast<Capture>();
    return keys.Zip(values, (k, v) => (key: k.Value, value: v.Value)).ToDictionary(p => p.key, p => p.value);
}

private static string FormatItem(Dictionary<string, string> value)
{
    if (value.Count == 0) return null;
    return "{" + string.Join("; ", value.Select(p => $"{p.Key}, {p.Value}")) + "}";
}

private static string FindChanges(string source, string target)
{
    var sourceItem = ParseItem(source);
    var targetItem = ParseItem(target);
    var result = new Dictionary<string, string>();
    foreach (var pair in targetItem)
    {
        if (!sourceItem.TryGetValue(pair.Key, out var value) || pair.Value != value)
        {
            result.Add(pair.Key, pair.Value);
        }
    }
    
    return FormatItem(result);
}
With that in place, you can adapt my previous answer to find the differences:
C#
var dictionary1 = new Dictionary<int, string>
{
    [1] = "{item 1, 10; item 2, 20; item 3, 40}",
    [2] = "{item 1, 15; item 2, 28; item 3, 40}",
    [3] = "{item 1, 15; item 2, 28; item 3, 40}",
};

var dictionary2 = new Dictionary<int, string>
{
    [1] = "{item 1, 12; item 2, 20; item 3, 40}",
    [2] = "{item 1, 15; item 2, 25; item 3, 40}",
    [3] = "{item 1, 15; item 2, 28; item 3, 40}",
};

var result = new Dictionary<int, string>();
foreach (var pair in dictionary2)
{
    if (!dictionary1.TryGetValue(pair.Key, out var value))
    {
        result.Add(pair.Key, pair.Value);
    }
    else if (value != pair.Value)
    {
        result.Add(pair.Key, FindChanges(value, pair.Value));
    }
}
Output:
plain
[1] = "{item 1, 12}"
[2] = "{item 2, 25}"




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

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 
QuestionHow to add text to a script tag at src using jQuery Pin
Member 1316600712-Jan-20 7:52
Member 1316600712-Jan-20 7:52 
AnswerRe: How to add text to a script tag at src using jQuery Pin
OriginalGriff12-Jan-20 10:25
mveOriginalGriff12-Jan-20 10:25 

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.