c# - How to move object between dictionaries? -
i have simple task need check objects in 1 dictionary , if criteria met move another. asking if there pattern can use language feature achieve that. straight approach simple - use temporaty collection, first step determine canditates, second step actual move. ok not cool.
current code
class order { public int id; public bool isready; } dictionary<int, order> activedictionary; dictionary<int, order> processeddictionary; public update() { // temporary list, uncool list<order> processed = new list<order>(); // fist step foreach(order ord in activedictionary) { if(ord.isready) { processed.add(ord); } } // ok lets move foreach(order ord in processed) { activedictionary.remove(ord.id); processeddictionary.add(ord.id, ord); } }
there nothing wrong code have.
as exercise in alternatives, like...
processeddictionary = processeddictionary .concat( activedictionary.where(kvp => kvp.value.ready) ) .todictionary(kvp => kvp.key, kvp => kvp.value); activedictionary = activedictionary.where(kvp => !kvp.value.ready) .todictionary(kvp => kvp.key, kvp => kvp.value);
Comments
Post a Comment