C#C
C#3y ago
SWEETPONY

✅ How to get only objects that changed?

I don't know why but arr3 contains 6 elements but should contains only 2
public class 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 class ChannelSettingsDto
{
    public string ChannelName { get; set; }

    public BehaviorType Behavior { get; set; }

    public TimeSpan Ttl { get; set; }
}

public enum BehaviorType
{
    [EnumMember(Value = "simple")]
    Simple,

    [EnumMember(Value = "requires_confirmation")]
    RequiresConfirmation,

    [EnumMember(Value = "requires_personal_confirmation")]
    RequiresPersonalConfirmation
}

var firstLst = new List<NotificationSettingsDto>()
{ 
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "123",
        IsEnabled = false,
    },
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "456",
        IsEnabled = false,
    },
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "789",
        IsEnabled = false,
    }
};

var secondLst = new List<NotificationSettingsDto>()
{
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "123",
        IsEnabled = true,
    },
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "456",
        IsEnabled = true,
    },
    new NotificationSettingsDto()
    {
        NotificationSettingsId = "789",
        IsEnabled = false,
    }
};

var arr1 = firstLst.Except(secondLst);
var arr2 = secondLst.Except(arr1);

var arr3 = arr1.Union(arr2).ToList();
Was this page helpful?