How to return the items that are difference in TWO DICTIONARIES (DICTIONARY<STRING, LIST<Runs>>) in c#?

Wanda 1 Reputation point
2023-11-01T15:05:25.3533333+00:00

Hello,

I have 2 dictionaries -- DICTIONARY<STRING, LIST<Details>>

internal class Details
        {
            
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public string Gender{ get; set; }
  }

old dictionary:

{"Mary", [{"2022-10-30", "2022-11-30", "F"}]}

{"Peter", [{"2022-11-30", "2022-12-30", "M"}]}

new dictionary:

{"Mary", [{"2022-10-30", "2022-12-30", "F"}]}

{"Peter", [{"2022-11-30", "2022-12-30", "M"}]}

As the example above, I want to return the item/list, as "EndDate" of the "Mary" in the new dictionary is different.

Is there a way to do it in C#? Please note the dictionary would be very large.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,094 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 47,501 Reputation points Microsoft Vendor
    2023-11-02T03:19:33.8866667+00:00

    Hi @Wanda , Welcome to Microsoft Q&A,

    You can use LinQ to write comparison code neatly and quickly.

    as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace _11_2_x
    {
        internal class Details
        {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public string Gender { get; set; }
        }
        internal class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, List<Details>> oldDictionary = new Dictionary<string, List<Details>>();
                Dictionary<string, List<Details>> newDictionary = new Dictionary<string, List<Details>>();
    
                // Initialize old and new dictionaries with your data
    
                // Example:
                oldDictionary["Mary"] = new List<Details>
            {
                new Details { StartDate = DateTime.Parse("2022-10-30"), EndDate = DateTime.Parse("2022-11-30"), Gender = "F" }
            };
                oldDictionary["Peter"] = new List<Details>
            {
                new Details { StartDate = DateTime.Parse("2022-11-30"), EndDate = DateTime.Parse("2022-12-30"), Gender = "M" }
            };
    
                newDictionary["Mary"] = new List<Details>
            {
                new Details { StartDate = DateTime.Parse("2022-10-30"), EndDate = DateTime.Parse("2022-12-30"), Gender = "F" }
            };
                newDictionary["Peter"] = new List<Details>
            {
                new Details { StartDate = DateTime.Parse("2022-11-30"), EndDate = DateTime.Parse("2022-12-30"), Gender = "M" }
            };
    
    
                var differentItems = newDictionary
                .Where(newKV =>
                    oldDictionary.TryGetValue(newKV.Key, out var oldDetailsList) &&
                    newKV.Value.Count == oldDetailsList.Count &&
                    newKV.Value.Zip(oldDetailsList, (newDetails, oldDetails) => new { New = newDetails, Old = oldDetails })
                        .Any(pair => pair.New.StartDate == pair.Old.StartDate && pair.New.EndDate != pair.Old.EndDate))
                .ToDictionary(kv => kv.Key, kv => kv.Value);
    
                foreach (var kvp in differentItems)
                {
                    Console.WriteLine($"Key: {kvp.Key} has different EndDates in newDictionary:");
                    foreach (var detail in kvp.Value)
                    {
                        Console.WriteLine($"StartDate: {detail.StartDate}, EndDate: {detail.EndDate}, Gender: {detail.Gender}");
                    }
                }
                Console.ReadLine();
            }
        }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.