I have three classes:
public class Error
{
public List<ErrorOccurrenceDetails> Occurrences { get; set; }
}
public class ErrorOccurrenceDetails
{
public Dictionary<DateTime, ErrorTime> Times { get; set; }
}
public class ErrorTime
{
public string Version { get; set; }
}
There's an Error that has multiple Occurrences that happened in different Times. Each time can occur on a different Version.
I have a list of Errors. I need to remove errors which do not have provided version, for example if user only wants to see errors in version "1.0", other versions (such as "1.1", "1.2") need to be removed. However, if an error has occurrences on version "1.0" AND others, filter should delete only bad occurrences, not entire error.
I tried to do it this way:
private IList<Error> errors;
private void Filter(string filterVersion)
{
var errorsToRemove = new Dictionary<int, ErrorOccurrenceDetails>();
foreach (Error error in this.errors)
{
int index = this.errors.IndexOf(error);
var listOfSearchedItems = error.Occurences.ToList();
listOfSearchedItems.RemoveAll(x => x.Times.Values.Any(y => y.Version != filterVersion));
var errorOccurences = error.Occurences.Except(listOfSearchedItems).ToList();
if (errorsToRemove.ContainsKey(index))
{
errorsToRemove[index].AddRange(errorOccurences);
}
else
{
errorsToRemove.Add(index, errorOccurences);
}
}
}
I'm trying to put all errors that need to be removed into errorsToRemove dictionary, where key is error index in this.errors, and value is a list of occurrences not on searched version.
It kinda works, but sometimes doesn't remove bad versions and causes some correct versions to also be removed, so there is a hole I haven't noticed.
Sample error list in this fiddle: https://dotnetfiddle.net/Kxq50i