C#C
C#3y ago
SWEETPONY

✅ How to find objects that changed?

I have following records:
public record NotificationSettingsDto
{
    public string? NotificationSettingsId { get; set; }
    public HashSet<string> EventCategories { get; set; }
    public HashSet<string> EventNames { get; set; }
    public HashSet<ChannelSettingsDto> ChannelSettings { get; set; }
    public bool IsEnabled { get; set; }
    public string TemplateMessage { get; set; }
}

public record ChannelSettingsDto
{
    public string ChannelName { get; set; }
    public BehaviorType Behavior { get; set; }
    public TimeSpan Ttl { get; set; }
}

public enum BehaviorType
{
    Simple,
    RequiresConfirmation,
    RequiresPersonalConfirmation
}


Than I create lists with objects and trying to find objects that was changed. I don't know why but changedItems.Count is 1
var firstLst = new List<NotificationSettingsDto>()
{ 
    new NotificationSettingsDto
    {
        NotificationSettingsId = "2",
        IsEnabled = false,
        EventCategories = new HashSet<string>(){"ac", "bc"},
        EventNames = new HashSet<string>(){"cc", "dd"},
        ChannelSettings = new HashSet<ChannelSettingsDto>()
        {
            new ChannelSettingsDto(){ ChannelName = "operator_notifications" }
        }
    }
};

var secondLst = new List<NotificationSettingsDto>()
{
    new NotificationSettingsDto
    {
        NotificationSettingsId = "2",
        IsEnabled = false,
        EventCategories = new HashSet<string>(){"ac", "bc"},
        EventNames = new HashSet<string>(){"cc", "dd"},
        ChannelSettings = new HashSet<ChannelSettingsDto>()
        {
            new ChannelSettingsDto(){ChannelName = "operator_notifications"}
        }
    }
};

var changedItems = firstLst
    .Join(secondLst,
        f => f.NotificationSettingsId,
        s => s.NotificationSettingsId,
        (f, s) => new {First = f, Second = s})
    .Where(pair => !pair.First.Equals(pair.Second))
    .ToList();
Was this page helpful?