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

  • SEARCH
  • Home
  • 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 667323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:54:55+00:00 2026-05-13T23:54:55+00:00

Often while I’m dealing with LINQ sequences, I want to send each item to

  • 0

Often while I’m dealing with LINQ sequences, I want to send each item to a method returning void, avoiding a foreach loop. However, I haven’t found an elegant way to do this. Today, I wrote the following code:

    private StreamWriter _sw;
    private void streamToFile(List<ErrorEntry> errors)
    {
        if (_sw == null)
        {
            _sw = new StreamWriter(Path.Combine
                                    (Path.GetDirectoryName(_targetDatabasePath), "errors.txt"));
        }

        Func<ErrorEntry, bool> writeSelector = 
            (e) => { _sw.WriteLine(getTabDelimititedLine(e)); return true; };

        errors.Select(writeSelector);

        _sw.Flush();
    }

As you can see, I write a lambda function that just returns true, and I realize that the Select method will return a sequence of booleans- I’ll just ignore that sequence. However, this seems a little bit noobish and jank. Is there any elegant way to do this? Or am I just misapplying LINQ?

Thanks.

  • 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-13T23:54:56+00:00Added an answer on May 13, 2026 at 11:54 pm

    First of all, your current code will not work.
    Select, and most other LINQ methods, use deferred execution, meaning that they don’t actually do anything until you enumerate the results.

    In general, you should never use a lambda with side effects in a LINQ query.

    To answer your question, you should use a foreach loop.

    You’re looking for a ForEach extension method; Eric Lippert explains why Microsoft didn’t write one.

    If you really want to, you can write one yourself:

    public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action) {
        if (sequence == null) throw new ArgumentNullException("sequence");
        if (action == null) throw new ArgumentNullException("action");
        foreach(T item in sequence) 
            action(item);
    }
    
    //Return false to stop the loop
    public static void ForEach<T>(this IEnumerable<T> sequence, Func<T, bool> action) {
        if (sequence == null) throw new ArgumentNullException("sequence");
        if (action == null) throw new ArgumentNullException("action");
    
        foreach(T item in sequence) 
            if (!action(item))
                return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often run into the situation where I want to disable some code while
While looking through the Java API source code I often see method parameters reassigned
In threads when dealing with cancelation, you often you see code like this while
While doing some refactoring I've found that I'm quite often using a pair or
While programming, I often come across the following scenario (described in code). I define
While striving for const-correctness, I often find myself writing code such as this class
While debugging jQuery apps that use AJAX, I often have the need to see
I often jump from a file to file, while working a large codebase. I
I often listen to music while I code. Switching songs while in the middle
While programming with python I am often confused with the following ambiguity: should it

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.