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 Query
- how do you execute a function on a List of objects using LINQ
- How to convert list of objects with two fields to array with one of them using LINQ
- How to apply a function to every element in a list using Linq in C# like the method reduce() in python?
- 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
- How can you use LINQ to cast from a collection of base class objects to a filtered list of their respective subclasses?
- how to execute custom function for each item later using linq deferred execution?
- How can I obtain objects from list with C# LINQ using boolean conditions?
- How to search a list of objects for a specific string attribute using LINQ in C#
- how to remove objects from list by multiple variables using linq
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- How do you populate a parent child data structure from XML where generic List<> objects are in parent and child structures using one LINQ query?
- how can I Select One row where max StartDate in list objects using linq
- How to convert a C# list of objects in a nested key:value format using LINQ in C#?
- Updating List using LINQ working when execute from Immediate window, not from code direct
- How do i group a list of dynamic objects using linq given that the property names are dynamic?
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- How do you perform a left outer join using linq extension methods
- 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
- How to select values within a provided index range from a List using LINQ
- How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
- Using LINQ to group a list of objects
- How to update value in a List using LINQ
- Change the property of objects in a List using LINQ
- How to update an element with a List using LINQ and C#
- Find child objects in list of parent objects using LINQ
- C# How to split a List in two using LINQ
- How do I get list of id's as int using LINQ
- Sort a list and all its nested objects using LINQ
More Query from same tag
- linq .Cast<> or cast inside ConvertAll for a list
- How could I take 1 more item from Linq's TakeWhile?
- EF core .any not filtering results
- Convert if-else within a foreach loop to LINQ
- How to solve compare two datatables linq to sql error?
- Linq Matching Pattern
- How to select the least count of items in fluent nHibernate via a linq query?
- LINQ to SQL and immutability
- How to port this Linq to VS 2005
- .Net Entity Framework - let keyword with a condition
- Building which properties to select with LINQ To Object
- How does one tell if a LINQ extension method is suffering from double enumeration?
- Order a list of string by quantity
- How to rewrite correlated join from SQL to LINQ
- What is LINQ equivalent of SQL
- Locking tables on read
- LINQ won't sum a group with a float? property
- Linq query with subquery as comma-separated values
- Percentage of a row in ordered query
- How to write a LINQ query if index usage is necessary?
- How to Zip one IEnumerable with itself
- Returning two Entity Framework query results together
- Linq group by multiple fields across different tables
- Foreach datarow filter in BIML
- Linq to c++.net expression conversion
- Lambda or LINQ for Complex Filter?
- Group by optimisation in C# on pre-sorted input
- Why Entity Framework Code First one to many Doesn't work properly
- Linq: check if one of the property in a list is null for all the objects
- CRM2011 - SDK 5.0.3 - Linq to CRM Entities Problem