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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:31:40+00:00 2026-05-18T10:31:40+00:00

I have following code. The query take some 30 seconds to execute. I found

  • 0

I have following code. The query take some 30 seconds to execute. I found that the top using block is called every time for each try to enumerate the iterator. Which mean query is executed for each call to MoveNext in enumerator.

So why lazy evaluation reintialize using over and over again. I know the work around but i need to understand the purpose of this.

   private IEnumerable<FriendProfile> GetProfiles() {
        var query = @"
            SELECT VirtualNumber, City, Gender, Nick, Age
            FROM Profiles
            WHERE Not VirtualNumber  In ( SELECT VirtualNumber FROM Messages)
            ORDER BY Id DESC;";

        using (var con = GetConnection()) {
            using (var cmd = dbconnection.CreateCommand()) {
                cmd.CommandText = query;
                var reader = cmd.ExecuteReader();
                while (reader.Read()) {
                    var fp = new FriendProfile();
                    fp.VirtualNumber = reader.GetString(0);
                    fp.City = reader.GetString(1);
                    fp.Gender = reader.GetString(2);
                    fp.Nick = reader.GetString(3);
                    fp.Age = reader.GetInt16(4).ToString();
                    yield return fp;

                }
            }
        }
    }


    foreach(var fp in GetProfiles()){
    .... //foreach item using(){} in GetProfile() is reinitializes. All usings blocks
    }
  • 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-18T10:31:40+00:00Added an answer on May 18, 2026 at 10:31 am

    I’m not 100% sure which using block you are referring to, but if I have understood correctly, what you have said should not be happening. The yield will return control of execution from the GetProfiles() method, but the code after the yield (ie. the end of the using blocks) will not be executed until the while condition is false.

    Here is a simple example that should demonstrate this behaviour:

    Using this class to show when the end of the using block executes:

    public class Disposable : IDisposable
    {
        private readonly string name;
    
        public Disposable(string name)
        {
            this.name = name;
        }
    
        public void Dispose()
        {
            Console.WriteLine("Disposing of {0}", name);
        }
    }
    

    and this code:

    private IEnumerable<int> Test()
    {
        using (new Disposable("outer"))
        {
            using (new Disposable("inner"))
            {
                for (int i = 0; i < 10; i++)
                {
                    yield return i;
                }
            }
        }
    }
    
    ...
    
    foreach (int i in Test())
    {
        Console.WriteLine("item {0}", i);
    }
    

    The output is:

    item 0
    item 1
    item 2
    item 3
    item 4
    item 5
    item 6
    item 7
    item 8
    item 9
    disposing of inner
    disposing of outer
    

    This shows that the using blocks do not exit until the for loop exits.

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

Sidebar

Related Questions

Today I found this query in our code that pulls up a list of
I have a friend who is using c# Linq to report on the following
I'll point out now, that I'm new to using saxon, and I've tried following
I'd like to avoid having many checks like the following in my code: myObj.someStringField
I have tried solving this problem by posting other related questions here that focused
Google Analytics use some query parameters to identify campaigns and to do cookie control.
Can anyone tell me if the following query calls the database multiple times or
I'm having a major performance issue with LINQ2SQL and transactions. My code does the
I am looking to combine the following queries into one, where scouting.jumpGate is integer,
I asked on SO a few days ago what was the simplest quickest way

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.