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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:13:20+00:00 2026-06-17T16:13:20+00:00

I am clue less about what has happend to performance of for loop when

  • 0

I am clue less about what has happend to performance of for loop when i tried to iterate through IEnumerable type.

Following is the code that cause serious performance issue

foreach (IEdge ed in edcol)
{
    IEnumerable<string> row = 
        from r in dtRow.AsEnumerable()
        where (((r.Field<string>("F1") == ed.Vertex1.Name) && 
                (r.Field<string>("F2") == ed.Vertex2.Name))
            || ((r.Field<string>("F1") == ed.Vertex2.Name) &&
                (r.Field<string>("F2") == ed.Vertex1.Name)))
        select r.Field<string>("EdgeId");
    int co = row.Count();
    //foreach (string s in row)
    //{

    //}
    x++;
}

The upper foreach(IEdge ed in edcol) has about 11000 iteration to complete.
It runs in fraction of seconds if i remove the line

int co = row.Count();

from the code.

The row.Count() have maximum value of 10 in all loops.

If i Uncomment the

//foreach (string s in row)
//{

//}

it goes for about 10 minutes to complete the execution of code.

Does IEnumerable type have such a serious performance issues.. ??

  • 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-06-17T16:13:21+00:00Added an answer on June 17, 2026 at 4:13 pm

    This answer is for the implicit question of “how do I make this much faster”? Apologies if that’s not actually what you were after, but…

    You can go through the rows once, grouping by the names. (I haven’t done the ordering like Marc has – I’m just looking up twice when querying 🙂

    var lookup = dtRow.AsEnumerable()
                      .ToLookup(r => new { F1 = r.Field<string>("F1"),
                                           F2 = r.Field<string>("F2") });
    

    Then:

    foreach (IEdge ed in edcol)
    {
        // Need to check both ways round...
        var first = new { F1 = ed.Vertex1.Name, F2 = ed.Vertex2.Name };
        var second = new { F1 = ed.Vertex2.Name, F2 = ed.Vertex1.Name };
        var firstResult = lookup[first];
        var secondResult = lookup[second];
    
        // Due to the way Lookup works, this is quick - much quicker than
        // calling query.Count()
        var count = firstResult.Count() + secondResult.Count();
    
        var query = firstResult.Concat(secondResult);
    
        foreach (var row in query)
        {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am absolute clueless why the following code keeps throwing NullpointerExceptions. I was not
No clue how to send an email as soon as an order has been
I have no clue what an anonymous type is in C# nor how it
I have a clue about Object Oriented Programming: I need to have a parent
Disclaimer: I'm completely clueless about .net and COM. I have a vendor's application that
While developing software, developers always tend to curse about some language specifics, improvements that
I'm wondering what could cause this. I have several methods in my code that
I'm in need of a software (not a service) that has a web API
I am managing a website which has about 140.000 documents. This system had been
I am clueless about regular expressions, but I know that they're the right tool

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.