Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 209977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:57:50+00:00 2026-05-11T17:57:50+00:00

I want to execute a function on all the objects within a List of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:57:50+00:00Added an answer on May 11, 2026 at 5:57 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Where are my Gems? You can find where your gems… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer There are two new "special" operators in this F# release,… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer Same problem here: AppendWith does only work for reserved keywords… May 11, 2026 at 7:03 pm

Related Questions

This is similar to Invoking a method using reflection on a singleton object but
So I've been working on this all day and I can't figure out how
I'd love some other opinions on what's more efficient in this code. Basically in
In a project that I'm working on I have to work with a rather
I find this excellent code, posted by aemkei as answers to this questions: How

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.