2011-04-29 13 views
6

我實現了一個ExtensionMethod基本上可以作爲的foreach循環的ForEach擴展方法,我的實現看起來是這樣的:爲ListItemCollection

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

不過,我想的方法停止在第一時間特定的循環後,條件得到滿足。

下面是我當前使用它:

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

這樣做的問題是,只能有相匹配這一要求將DropDownList內的一個項目,但循環被完成,無論如何,這將是最不醜陋的方式來解決這個問題?

謝謝。

+2

爲什麼不使用** Find **? – V4Vendetta 2011-04-29 10:43:04

+3

如果您沒有試驗** ddlProcesses.Items.FindByValue(Request [「Process」])。Selected = true; **將是最簡單的事情 – V4Vendetta 2011-04-29 10:52:54

+0

@ V4Vendetta:使用波浪線突出顯示評論中的語法 – abatishchev 2011-04-29 10:55:59

回答

5

你可以採取Func<ListItem, bool>而不是Action<ListItem>並打破循環,如果它返回true

public static void ForEach(this ListItemCollection collection, 
    Func<ListItem, bool> func) 
{ 
    foreach (ListItem item in collection) { 
     if (func(item)) { 
      break; 
     } 
    } 
} 

您可以使用它像這樣:

ddlProcesses.Items.ForEach(
    item => item.Selected = (item.Value == Request["Process"])); 
1

首先做到這一點:

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

獲得泛型集合。

然後use can roll your own, generic 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; 
} 

無論如何,緩存查找結果:

var process = Request["Process"]; 

e.ForEach(i => i.Selected = i.Value == process); 
+0

e.ToList()導致在迭代之前將整個列表複製到新的List實例中。這可能不合意。 – 2011-04-29 10:55:09

2
public static void ForEach(this ListItemCollection collection, Action<ListItem> act) 
{ 
    foreach (ListItem item in collection) 
    { 
     act(item); 
     if(condition) break; 
    } 
} 
+0

條件從何而來? – 2011-04-29 10:53:04

+0

@abatischev這是來自OP「我想在第一次滿足特定條件後停止循環的方法。」 – Gabriel 2011-04-29 10:54:56

+0

@Peter OP可以把他的病情放在那裏...我只是想讓他知道你可以停下來休息一下foreach循環; – Gabriel 2011-04-29 10:56:18

1

你的要求是不是100%清楚。你是否需要處理所有項目以將它們設置爲false,除了唯一一個與條件匹配的項目,還是隻想找到具有正確條件的項目,還是希望在滿足條件前應用函數?

  1. 只有做一些項目首次條件匹配

    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; 
         } 
        } 
    } 
    
  2. 做些事情來的物品,每次一個條件匹配

    public static void ApplyIf(this ListItemCollection collection, 
             Action<ListItem> action, Func<ListItem, bool> predicate) 
    { 
        foreach (ListItem item in collection) 
        { 
         if (predicate(item)) 
         { 
          action(item); 
         } 
        } 
    } 
    
  3. 做些事情來的所有項目,直到條件匹配

    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; 
         } 
        } 
    } 
    
相關問題