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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:44:12+00:00 2026-05-31T16:44:12+00:00

These 2 ways of working both work, but I’m wondering if there’s a difference

  • 0

These 2 ways of working both work, but I’m wondering if there’s a difference in performance:

Dim collection As ItemCollection = CType(CellCollection.Where(Function(i) i.IsPending = True), ItemCollection)
For Each item As Item In collection
    'Do something here
Next

and

For Each item As Item In CellCollection.Where(Function(i) i.IsPending = True)
    'Do something here
Next

I thought the second one was better as you’d have a variable less and looks cleaner, but on second thought, I’m not quite sure what happens when you put a linq query in the iteration.

Does it have to reevaluate every time a loop is made? And which one is the cleanest/most performant to use?

Thanks in advance.

  • 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-31T16:44:13+00:00Added an answer on May 31, 2026 at 4:44 pm

    I’ve created a simple test console app.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    
    namespace LinqPerformance
    {
        class Program
        {
            static void Main(string[] args)
            {
                var data = Enumerable.Range(1, 100000000);
    
                for (int x = 0; x < 10; x++)
                {
                    ExecuteMethods(data);
                }
            }
    
            private static void ExecuteMethods(IEnumerable<int> data)
            {
                Method1("linq collection", () =>
                {
                    var collection = data.Where(d => d % 2 == 0);
                    double count = 0;
    
                    foreach (var c in collection)
                    {
                        count += c;
                    }
                });
    
                Method1("list collection", () =>
                {
                    var collection = data.Where(d => d % 2 == 0).ToList();
                    double count = 0;
                    foreach (var c in collection)
                    {
                        count += c;
                    }
                });
    
                Method1("iterable collection", () =>
                {
                    double count = 0;
                    foreach (var c in data.Where(d => d % 2 == 0))
                    {
                        count += c;
                    }
                });
            }
    
            private static void Method1(string name, Action body)
            {
                Stopwatch s = new Stopwatch();
    
                s.Start();
                body();
                s.Stop();
    
                Console.WriteLine(name + ": " + s.Elapsed);
            }
        }
    }
    

    After running this I can see that the ToList() is the slowest. The other two approaches appear to be the same.

    I suppose this is because the foreach is expanded to a

    var enumerator = collection.GetEnumerator();
    
    while(enumerator.MoveNext() )
    {
        var c = enumerator.Current;
        count += c;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if these are improper ways of using LINQ. They work, just don't
I'm working on one of those projects where there are a million better ways
What is the difference between these to ways of table creation. CREATE TABLE TABLENAME(
Why can't I define a workbook either of these ways? (I have the range
I have seen quite a few ways to output variables. Which of these ways
Is one of these two ways to create and initialize an object preferable? MyClass
I have tried all of these various ways to set the value of the
I am trying to understand the trade offs/differences between these to ways of opening
These are the three ways in which the same image has been referenced to
I have an ecommerce working in ASP.Net MVC. I'm using Caching to improve performance

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.