I want to execute a function on all the objects within a List of objects using LINQ. I know i saw something similar on SO before but after several failed search attempts, i am posting this question
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try the following if it is actually of type
List<T>.C#
VB.Net
Unfortunately .ForEach is only defined on
List<T>so it cannot be used on any generalIEnumerable<T>type. Although it’s easy to code such a functionC#
VB.Net
With this you can run .ForEach on any
IEnumerable<T>which makes it usable from practically any LINQ query.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