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();
}
}
}
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.