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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:55:02+00:00 2026-05-28T00:55:02+00:00

I have some behavior that I don’t understand. I’m using RavenDB, and I’m using

  • 0

I have some behavior that I don’t understand. I’m using RavenDB, and I’m using a session for each unit of work: When a logic class calls the RavenDB data access layer (DAL), a new session is created. Within the DAL, other DAL classes and methods can be invoked, but just one session will be used.

The part I don’t understand is the difference between using IEnumerable and List within the GetMostRecentByStartTime() method below. In that method, using List just like what’s shown, this is my output:

Using List:
Number of requests just before closing session: 2
Number of requests just before closing session: 4
Number of requests just before closing session: 6
Number of requests just before closing session: 7

Note: The session doesn’t actually get closed each of these times; only after the last time. We only close the session when the originally-called DAL method is done.

Now, if I replace every instance of List with IEnumerable (and that’s the only change I make), I get this output:

Using IEnumerable:
Number of requests just before closing session: 2
Number of requests just before closing session: 3
Number of requests just before closing session: 4
Number of requests just before closing session: 27

Why the difference?

Another issue is that the request count increases, using the IEnumerable approach, when I add new InstallationSummary objects within my app. I don’t understand why. When I use the List approach, the request count stays the same, even after adding more InstallationSummary objects. Can anyone explain that, too?

public IEnumerable<InstallationSummary> GetMostRecentByStartTime(int numberToRetrieve)
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    try
    {
        return ExecuteQuery<IEnumerable<InstallationSummary>>(() =>
        {
            List<InstallationSummary> installationSummaries =
                QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
                .Include(x => x.ApplicationServerId)
                .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
                .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
                .OrderByDescending(summary => summary.InstallationStart)
                .Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();

            List<string> appServerIds = (from item in installationSummaries select item.ApplicationServerId).ToList();
            List<string> appIds = (from item in installationSummaries select item.ApplicationWithOverrideVariableGroup.ApplicationId).ToList();
            List<string> groupIds = (from item in installationSummaries select item.ApplicationWithOverrideVariableGroup.CustomVariableGroupId).ToList();

            List<ApplicationServer> appServers = new ApplicationServerData().GetByIds(appServerIds).ToList();
            List<Application> apps = new ApplicationData().GetByIds(appIds).ToList();
            List<CustomVariableGroup> groups = new CustomVariableGroupData().GetByIds(groupIds).ToList();

            foreach (InstallationSummary summary in installationSummaries)
            {
                summary.ApplicationServer = appServers.Where(server => server.Id == summary.ApplicationServerId).FirstOrDefault();

                summary.ApplicationWithOverrideVariableGroup.Application =
                    apps.Where(app => app.Id == summary.ApplicationWithOverrideVariableGroup.ApplicationId).FirstOrDefault();

                if (summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId == null) { continue; }

                summary.ApplicationWithOverrideVariableGroup.CustomVariableGroup =
                    groups.Where(group => group.Id == summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId).FirstOrDefault();
            }

            return installationSummaries;
        });
    }
    finally
    {
        stopwatch.Stop();
        Debug.WriteLine("InstallationSummaryData.GetMostRecentByStartTime(): " + stopwatch.ElapsedMilliseconds);
    }
}

Here is where the above method gets invoked:

protected T ExecuteQuery<T>(Func<T> func)
{
    if (func == null) { throw new ArgumentNullException("func"); }

    try
    {
        return func.Invoke();
    }
    finally
    {
        Debug.WriteLine("Number of requests just before closing session: " + _session.Advanced.NumberOfRequests);
        CloseSession();
    }
}
  • 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-28T00:55:03+00:00Added an answer on May 28, 2026 at 12:55 am

    First, it is not a minor change if you swap all your ILists out and replace with IEnumerables. The major difference is that you can get deferred execution with your IEnumerables, while you always have eager execution when using IList. This can cause a lot of issues, if you’re not aware of this difference.

    In your case, the reason for the difference is a combination of deferred execution and wrong usage of the .Include<T>() feature of RavenDB. The latter is intended to reduce the number of remote calls to the database by caching the included documents inside the clients DocumentSession. This works great if you use DocumentSession.Load<T>() but it doesn’t make any difference if you get your documents using DocumentSession.Query<T>().Where(x => x.Id == id). If you are familiar with with NHibernate, this is your first level cache.

    In order to get it working, change your code and use this instead:

    List<InstallationSummary> installationSummaries =
        QueryAndCacheEtags(session => session.Advanced.LuceneQuery<InstallationSummary>()
        .Include(x => x.ApplicationServerId)
        .Include(x => x.ApplicationWithOverrideVariableGroup.ApplicationId)
        .Include(x => x.ApplicationWithOverrideVariableGroup.CustomVariableGroupId)
        .OrderByDescending(summary => summary.InstallationStart)
        .Take(numberToRetrieve)).Cast<InstallationSummary>().ToList();
    
    foreach (InstallationSummary summary in installationSummaries)
    {
        summary.ApplicationServer = session.Load<ApplicationServer>(summary.ApplicationServerId);
    
        summary.ApplicationWithOverrideVariableGroup.Application =
            session.Load<Application>(summary.ApplicationWithOverrideVariableGroup.ApplicationId);
    
        if (summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId != null)
            summary.ApplicationWithOverrideVariableGroup.CustomVariableGroup =
                session.Load<CustomVariableGroup>(summary.ApplicationWithOverrideVariableGroup.CustomVariableGroupId);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered some strange Perl behavior: using a Posix character class in a
I have some extremely weird behavior that seems to result in silent exceptions. How
I'm noticing some behavior that I don't like, and I'm wondering if this is
I have some javascript click handlers that don't do what I want in IE8.
I have come across a strange behavior with Microsoft Code Contracts that I don't
I would like to create a file whose descriptor would have some customizable behavior.
Ok, I have some rather odd behavior in one of my Projects and I'm
We have some software which relied on certain behavior from another ( very commonly
I've experienced rather strange behavior of JSTL forEach tag. I have some bean called
I have developed some classes with similar behavior, they all implement the same interface.

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.