score:5
You can take a Func<ListItem, bool>
instead of an 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 like this:
ddlProcesses.Items.ForEach(
item => item.Selected = (item.Value == Request["Process"]));
score:1
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);
score:1
Your requirements are not 100% clear. 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?
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; } } }
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); } } }
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; } } }
score:2
public static void ForEach(this ListItemCollection collection, Action<ListItem> act )
{
foreach (ListItem item in collection)
{
act(item);
if(condition) break;
}
}
Source: stackoverflow.com
Related Query
- ForEach Extension Method for ListItemCollection
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Linq extension method equivalent for that code in VB.NET
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- IQueryable<T> does not contain a definition for 'Include' and no extension method 'Include'
- Is there any method like ForEach for IList?
- What is the performance of the Last() extension method for List<T>?
- Extension method for IQueryable left outer join using LINQ
- Average extension method in Linq for default value
- How can I write a generic extension method for converting a delimited string to a list?
- System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of
- IQueryable extension method for linq2entities
- LINQ - Writing an extension method to get the row with maximum value for each group
- 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>' could be found
- Lambda Extension Method for Converting IEnumerable<T> to List<SelectListItem>
- SQL equivalent of Count extension method for LINQ isn't obvious
- ForEach extension method
- Is there a way to specify which extension method to use for extension methods of the same name in different namespaces?
- Count and ToList extension method for Enum?
- Generic Extension method for LINQ JOIN using 2 columns
- How-to use predicates for customer where LINQ query in Entity Framework, Extension Method "Search by Words"
- Extension Method not compiling (No defintion for type 'string')
- Is LINQ Where extension method optimized for SortedDictionary?
- Does not contain a definition for 'where' and the best extension method overload
- CS1061: 'IEnumerable<>' does not contain a definition for '' and no extension method '' accepting a first argument of type 'IEnumerable<>'
- What can be the code for someIEnumerable.Select extension method?
- Linq Extension method for Join
- Multiple Selection in ForEach Extension Method
- 'int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains
- LINQ extension method for multiple join with multiple GroupBy requirements
More Query from same tag
- Stripping characters from PredicateBuilder to improve search accuracy
- Linq query C# How to add condition below query
- linq join 1 to many get first record
- Cross table / object update with (Db)LINQ
- C# xml file sorting tags by id, but saving same name order
- How to get maximum Id in List and then return list
- EF - Linq Expression and using a List of Ints to get best performance
- What would be a reasonably fast way to code this sql query in c#?
- Linq ToList does nothing
- List unique transformation in LINQ
- c# loop through linq results and update field
- How to compare array of elements with the linq query result in c#
- IntelliSense dosen't give me option to select properties after implementing the LINQ
- Sorting a list of objects based on another
- Details' View with related data via LINQ using a custom model
- C# Linq to VB.NET (not a From In Where ... query style)
- LINQ to SQL join tables special blend
- How to add multiple records in List along with other model in view-model Linq - ASP.NET-MVC5
- Can an IGrouping enumeration be bound in XAML? And if so, what would the binding syntax look like?
- LINQ-to-SQL: Searching against a CSV
- C# where does the dbml file come from?
- convert the paramater's date type to varchar , of stored procedure in linq query
- Case Condition with GroupBy in Linq for duplicate records
- LINQ select a date range to join on other queries
- LINQ: Assinging Property-values in IEnumerable<T>
- Get all objects that have an object that matches a string
- Remove entries from dictionary using LINQ - locking
- select from grouped collection
- Linq Join Duplicates records c#
- Problem transforming dictionary with Linq