c# - ForEach Extension Method for ListItemCollection -


i implemented extensionmethod works foreach-loop, implementation looks this:

public static void foreach(this listitemcollection collection, action<listitem> act ) {     foreach (listitem item in collection)         act(item); } 

however, i'd method stop looping after first time specific condition met.

here's how use it:

ddlprocesses.items.foreach(item => item.selected = item.value == request["process"]?true:false); 

the problem there can 1 item inside dropdownlist matches requirement, loop being finished anyway, least ugly way solve problem?

thanks.

you can take func<listitem, bool> instead of action<listitem> , break loop if returns true:

public static void foreach(this listitemcollection collection,     func<listitem, bool> func) {     foreach (listitem item in collection) {         if (func(item)) {             break;         }     } } 

you can use this:

ddlprocesses.items.foreach(     item => item.selected = (item.value == request["process"])); 

Comments