score:14
Try the following if it is actually of type List<T>
.
C#
var list = GetSomeList();
list.ForEach( x => SomeMethod(x) );
' Alternatively
list.ForEach(SomeMethod);
VB.Net
Dim list = GetSomeList();
list.ForEach( Function(x) SomeMethod(x) );
Unfortunately .ForEach is only defined on List<T>
so it cannot be used on any general IEnumerable<T>
type. Although it's easy to code such a function
C#
public static void ForEach<T>(this IEnumerable<T> source, Action<T> del) {
foreach ( var cur in source ) {
del(cur);
}
}
VB.Net
<Extension()> _
Public Sub ForEach(Of T)(source As IEnumerable(Of T), ByVal del As Action(Of T)
For Each cur in source
del(cur)
Next
End Sub
With this you can run .ForEach on any IEnumerable<T>
which makes it usable from practically any LINQ query.
var query = from it in whatever where it.SomeProperty > 42;
query.ForEach(x => Log(x));
EDIT
Note to use the .ForEach for VB.Net. You must chose a function that returns a value. It's a limitation of lambda expressions in VB.Net 9 (VS 2009). But there is o work around. Say you want to call SomeMethod which is a Sub. Simply create a wrapper which returns an empty value
Sub SomeMethod(x As String)
...
End Sub
Function SomeMethodWrapper(x As String)
SomeMethod(x)
Return Nothing
End Function
list.ForEach(Function(x) SomeMethod(x)) ' Won't compile
list.ForEach(function(x) SomeMethodWrapper(x)) ' Works
Source: stackoverflow.com
Related Articles
- how do you execute a function on a List of objects using LINQ
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Searching if value exists in a list of objects using Linq
- Using Linq to group a list of objects into a new grouped list of list of objects
- Using LINQ to group a list of objects
- Change the property of objects in a List using LINQ
- Find child objects in list of parent objects using LINQ
- Sort a list and all its nested objects using LINQ
- Using LINQ to merge a list of objects
- Using LINQ to Get Sum/ Average of a List with custom objects
- How to convert list of objects with two fields to array with one of them using LINQ
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- Linq To XML - Using XDocument and creating list of objects
- Query in returning Objects from List using LINQ
- Searching a List of objects in an object list using linq in VB.net
- sorting list of objects with null properties using linq
- How to apply a function to every element in a list using Linq in C# like the method reduce() in python?
- Filter a list of objects using LINQ
- Using LINQ to get the difference between two list of objects based only on a single object property
- LINQ - Sort List of objects using OrderBy EnumValue, and Union result set?
- LINQ query using list of objects property of an object
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- How to get a complement list of objects using linq and EntityFramework
- Sort a list of Person objects using Linq
- List of EF objects to Dictionary of lists using linq
- Read xml using LINQ and store in a list of objects
- Using linq group by to get from a list of objects with a DateTime property to a list with an interval StartDate, EndDate
- Extract list of objects from a string using linq
- C# Using LINQ to filter each Object with Max Value in List of Objects
- Get the objects with the maximum value for a specific property from a list using LINQ
- Calculate total number of threads in the operating system(Windows/Linux)
- Linq XML Read Element with Attribute
- SQL command into linq expression entity framework
- Can you add an If Statement in a LINQ(XML) query?
- Trouble with PredicateBuilder
- LINQ to return true if collection contains element satisfying predicate
- Linq to XML conditioning attribute
- Case Insensitive Array.Contains in Linq Query
- LINQ and web service cannot return anonymous types, and you cannot construct an object in a query?
- Converting SQL to LINQ query
- Find the highest small number in List <int>
- Unknown column 'Project2.Name' in 'where clause'
- making a list using inner query in Linq in C#
- NDepend: how to export the result from a query
- Limit query to ignore elements descendants
- Is it possible to use LINQ to detect duplicate adjacent characters?
- How to dynamically create Joins in LINQ?
- How to make EF efficiently call an aggregate function?
- linq to sql select inside an inner join
- Deserialize JSON in c# when the return dataset in not known