简体   繁体   English

ListItemCollection 的 ForEach 扩展方法

[英]ForEach Extension Method for ListItemCollection

I implemented an ExtensionMethod which basically works as ForEach-Loop, my Implementation looks like this:我实现了一个 ExtensionMethod,它基本上作为 ForEach-Loop 工作,我的实现如下所示:

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

However, I'd like the method to stop looping after the first time a specific condition is met.但是,我希望在第一次满足特定条件后停止循环的方法。

Here's how I currently use it:这是我目前使用它的方式:

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

The Problem with this is that there can only be one item inside the DropDownList which matches this requirement, but the loop is being finished anyway, what would be the least ugly way to solve this problem?这样做的问题是 DropDownList 中只能有一个项目符合此要求,但循环无论如何都已完成,解决此问题的最不难看的方法是什么?

Thanks.谢谢。

You can take a Func<ListItem, bool> instead of an Action<ListItem> and break the loop if it returns true :您可以使用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;
        }
    }
}

You can use it like this:你可以像这样使用它:

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

First do this:首先这样做:

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

to get generic collection.获得通用集合。

Then 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;
}

Anyway, cache lookup result:无论如何,缓存查找结果:

var process = Request["Process"];

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

Your requirements are not 100% clear.您的要求不是 100% 明确的。 Do you need to process all items to set them to false except the only one which matches the condition or do you just want to find the with the right condition or do you want to apply a function until a condition is met?您是否需要处理所有项目以将它们设置为 false 除了唯一符合条件的项目,还是您只想找到具有正确条件的项目,或者您想应用 function 直到满足条件?

  1. Only do something to an 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; } } }
  2. Do something to an item everytime a 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); } } }
  3. Do something to all items until a 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; } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM