ForEach extension method for ListItemCollection

I implemented ExtensionMethod, which basically works like ForEach-Loop, my implementation looks like this:

public static void ForEach(this ListItemCollection collection, Action<ListItem> act ) { foreach (ListItem item in collection) act(item); } 

However, I would like the method to stop the loop after the first fulfillment of a certain condition.

Here's how I use it now:

 ddlProcesses.Items.ForEach(item => item.Selected = item.Value == Request["Process"]?true:false); 

The problem is that there can only be one element inside DropDownList that meets this requirement, but the loop is still finished, what would be the least ugly way to solve this problem?

Thanks.

+6
source share
4 answers

You can take Func<ListItem, bool> instead of Action<ListItem> and break the loop if it 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 it as follows:

 ddlProcesses.Items.ForEach( item => item.Selected = (item.Value == Request["Process"])); 
+5
source
 public static void ForEach(this ListItemCollection collection, Action<ListItem> act ) { foreach (ListItem item in collection) { act(item); if(condition) break; } } 
+2
source

First do the following:

 IEnumerable<ListItem> e = ddlProcesses.Items.OfType<ListItem>(); // or Cast<ListItem>() 

to get a common set.

Then it can use its own general extension method :

 public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) { foreach (T item in collection) action(item); } public static void ForEach<T>(this IEnumerable<T> collection, Func<T> func) { foreach (T item in collection) if (func(item)) return; } 

In any case, the search result in the cache:

 var process = Request["Process"]; e.ForEach(i => i.Selected = i.Value == process); 
+1
source

Your requirements are not 100% understandable. Do you need to process all the elements to set them to false, except for the only one that matches the condition, or do you just want to find a suitable condition or do you want to apply the function until the condition is met?

  • Do just something for the item the first time the condition matches.

     public static void ApplyFirst(this ListItemCollection collection, Action<ListItem> action, Func<ListItem, bool> predicate) { foreach (ListItem item in collection) { if (predicate(item)) { action(item); return; } } } 
  • Do something for the item every time the condition matches

     public static void ApplyIf(this ListItemCollection collection, Action<ListItem> action, Func<ListItem, bool> predicate) { foreach (ListItem item in collection) { if (predicate(item)) { action(item); } } } 
  • Do something to all elements until condition matches

     public static void ApplyUntil(this ListItemCollection collection, Action<ListItem> action, Func<ListItem, bool> predicate) { foreach (ListItem item in collection) { action(item); if (predicate(item)) { return; } } } 
+1
source

Source: https://habr.com/ru/post/886952/


All Articles