C
C#11mo 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
}
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();
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();
6 Replies
Jimmacle
Jimmacle11mo ago
you aren't defining equality for your types so it defaults to reference equality
SWEETPONY
SWEETPONY11mo ago
hmmm I'll try to use records instead
Jimmacle
Jimmacle11mo ago
that would be the easiest solution
SWEETPONY
SWEETPONY11mo ago
now it works, thanks!
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX11mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server