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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:23:16+00:00 2026-05-29T22:23:16+00:00

I have a supplying method in DAL: public IEnumerable<RecordType> GetRecords() { using (LinqDataContext context

  • 0

I have a supplying method in DAL:

public IEnumerable<RecordType> GetRecords()
{
    using (LinqDataContext context = new LinqDataContext())
    {
        var records = context.RecordTable;

        foreach (RecordType record in records)
        {
            yield return record;
        }
    }
}

The consumer method uses the records in a foreach loop. This way I want to save a memory usage by not caching all the records from RecordTable, since LinqToSql uses a DataReader behind the scene.

I have two questions on this scenario though:

  1. Is that true that yield-returning above saves resources and works faster than caching all records to an array (.ToArray())?

  2. Will the data connection be automatically properly closed (I mean the using statement) if an error will occur inside the foreach loop of the consuming method OR if the consuming method will break the foreach loop in the middle (like found a required record and break)?

  • 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-29T22:23:16+00:00Added an answer on May 29, 2026 at 10:23 pm

    In the case of executing a basic query, it can work that way (certainly it is possible) – however, in the case of querying a naked Table<T>, it might be that it all buffers first; you could perhaps try querying the count during the iteration, or running a trace. In this case I suspect it will buffer first.

    Re closed: that also depends ;p If someone is using foreach, then yes: since foreach explicitly disposes the iterator via finally. However! It is not guaranteed if someone does, for example (very naughty and lax):

    var iter = yourData.GetEnumerator();
    if(iter.MoveNext()) {
        Console.WriteLine(iter.Current.Name); // first record of, say, 20
    }
    // and don't dispose the iterator == bad
    

    then since the iterator doesn’t a: get disposed, b: exhaust itself, and c: doesn’t crash, it won’t shut down properly (any of those 3 conditions will close it properly). Emphasis: this is a pathological case: normally it is reasonably safe to say “it will close, yes”.

    If you want guaranteed non-buffering, note that “dapper” has that, if you set buffered to false:

    IEnumerable<Customer> customers = connection.Query<Customer>(
           "select * from Customer", buffered: false);
    

    (it can also handle the parameters etc)

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

Sidebar

Related Questions

Supposing I have this code public class Class1 { List<Class2> myClass2List = new List<Class2>;
I have a Python list that I'm supplying to the template: {'error_name':'Please enter a
I have no clue how to validate this string. I am simply supplying an
Is there a way of supplying enum values as method-args in pococapsule without resorting
I'm fairly new to OO programming specifically with Java. I have a question pertaining
Supposing I have the following LambdaExpression: var itemParam = Expression.Parameter(typeof(Thing), thing); var someValue =
I have a method of an object which creates objects which are then passed
I have a custom HtmlHelper; public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave
I have created a dialog which can be called from anywhere by supplying a
I wanted to have an optional date parameter for a method (defaulted to MinValue),

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.