kind of a noob question, i am trying to compare elements of a given List with other elements of the same list. Since i am trying to compare 2 classes to verify if they are identical and then remove them from the list. I am using the Json Serializer to compare two strings.
The list allMeetings is comprised of several istances of the class Meeting wich has some methods, i am trying to compare those element with eachother, to prevent elements to be repeating, so that when i write to a file i dont get double my data.
public void saveOnClosing() {
foreach (var item in Events)
{
allMeetings.Add(item);
}
for(int i = 1; i <= allMeetings.Capacity; i++)
{
if (i - 1 == -1) { continue; }
else {
if JsonConvert.SerializeObject(allMeetings[i]) == JsonConvert.SerializeObject(allMeetings[i - 1]))
{
allMeetings.RemoveAt(i);
}
}
}
using (StreamWriter r = new StreamWriter("JsonInput.json"))
{
r.Write(JsonConvert.SerializeObject(allMeetings));
}
}
This solution i tried to implement results in a WPF exception "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name'
" , any help is apreciated
Alp3x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.