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 6827955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:19:35+00:00 2026-05-26T22:19:35+00:00

When i use a standard Extension Method on a List such as Where (…)

  • 0

When i use a standard Extension Method on a List such as
Where(…)

the result is always IEnumerable, and when
you decide to do a list operation such as Foreach()

we need to Cast(not pretty) or use a ToList() extension method that

(maybe) uses a new List that consumes more memory (is that right?):

List<string> myList=new List<string>(){//some data};

(Edit: this cast on’t Work)

myList.Where(p=>p.Length>5).Tolist().Foreach(...);

or

(myList.Where(p=>p.Length>5) as List<string>).Foreach(...);

Which is better code or is there a third way?

Edit:
Foreach is a sample, Replace that with BinarySerach

myList.Where(p=>p.Length>5).Tolist().Binarysearch(...)
  • 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-26T22:19:36+00:00Added an answer on May 26, 2026 at 10:19 pm

    The as is definitely not a good approach, and I’d be surprised if it works.

    In terms of what is “best”, I would propose foreach instead of ForEach:

    foreach(var item in myList.Where(p=>p.Length>5)) {
        ... // do something with item
    }
    

    If you desperately want to use list methods, perhaps:

    myList.FindAll(p=>p.Length>5).ForEach(...);
    

    or indeed

    var result = myList.FindAll(p=>p.Length>5).BinarySearch(...);
    

    but note that this does (unlike the first) require an additional copy of the data, which could be a pain if there are 100,000 items in myList with length above 5.

    The reason that LINQ returns IEnumerable<T> is that this (LINQ-to-Objects) is designed to be composable and streaming, which is not possible if you go to a list. For example, a combination of a few where / select etc should not strictly need to create lots of intermediate lists (and indeed, LINQ doesn’t).

    This is even more important when you consider that not all sequences are bounded; there are infinite sequences, for example:

    static IEnumerable<int> GetForever() {
        while(true) yield return 42;
    }
    var thisWorks = GetForever().Take(10).ToList();
    

    as until the ToList it is composing iterators, not generating an intermediate list. There are a few buffered operations, though, like OrderBy, which need to read all the data first. Most LINQ operations are streaming.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For those of us who use standard shared hosting packages, such as GoDaddy or
You can use a standard dot notation or a method call in Objective-C to
We're planning to use standard ASP.NET user authentication for our application. However, by default
I would like to use standard ASP.NET file download response, like in other Stack
I would like to use standard icons in treeview but I am not sure
I just noticed that you can not use standard math operators on an enum
If for example I should not use standard library functions like printf() , putchar()
In my asp mvc application I use standard client side validation (DataAnnotations + MicrosoftAjax.js
Our company builds custom Java EE web solutions. At the moment, we use standard
How can I use the standard Edit menu in my Palm OS application, instead

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.