Is there an easy way to influence the processing sequence of an ObservableCollection in c#?
Lets say I have the following class:
public class GeneratorObject : IComparable
{
int processingSequence {get; set;}
string property1 {get; set;}
Boolean property2 {get; set;}
public GeneratorObject(int processingSequence)
{
this.processingSequence = processingSequence;
}
public int CompareTo(object obj)
{
// Implementation
}
}
The processing sequence could be every natural number.
I want to iterate some of some of them
ObservableCollection<GeneratorObject> toIterate = new ObservableCollection<GeneratorObject>();
toIterate.Add(new GeneratorObject(99));
toIterate.Add(new GeneratorObject(1));
toIterate.Add(new GeneratorObject(10));
toIterate.Add(new GeneratorObject(6));
foreach (GeneratorObject field in toIterate)
{
// Manipulating the properties of field
}
I dont really want to sort the Collection, espeacially in the view. I just want to change the order for the iteration. First the element with the lowest sequence number, ..., Furthermoore it is important to say, that im manipulating the items while iterating and could not just use a copy of the collection.
Ive already implemented IComparable and thought i could easy say e.g. Collection.Sort() without destroying the binding with the UI.
Thanks for any help.
Thanks for the hint with the copy. I dont know if this is a good answer (especially regarding to performce), but i solved the problem the following way.
var copy = toIterate.ToList();
copy.Sort();
// iterate through the sorted List
foreach (GeneratorObject field in copy)
{
// get the original object regarding to this sorted item
GeneratorObject originalObjectForAction = getGeneratorObject(toIterate, field);
// do stuff with the original item
}
public GeneratorObject getGeneratorObject(ObservableCollection<GeneratorObject> list, GeneratorObject objekt)
{
foreach (DwhRoboterGeneratorObjekt field in list)
{
if (field == objekt) return field;
}
throw new Exception("Not found");
}