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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:34:39+00:00 2026-06-11T02:34:39+00:00

I store User objects in RavenDB. Each User has a User.Id property. I also

  • 0

I store User objects in RavenDB. Each User has a User.Id property.

I also have a Relationship class that links two User.Ids together to create a Mentor/Mentee relationship, like this:

public class User
{
    public string Id { get; set; }
    public string UserName { get; set; }
    ... more properties
}

public class Relationship
{
    public string Id { get; set; }
    public string MentorId { get; set; }
    public string MenteeId { get; set; }
    public RelationshipStatus Status { get; set; }
}

Now I want to retrieve a list of Mentees for a given Mentor. I have done this in the following way:

public static List<User> GetMentees(IDocumentSession db, string mentorId)
{
    var mentees = new List<User>();
    db.Query<Relationship>()
            .Where(r => r.MentorId == mentorId)
            .Select(r => r.MenteeId)
            .ForEach(id => mentees.Add(db.Load<User>(id)));
    return mentees;
}

This seems to work just fine but the coding-angel on my shoulder is wrinkling her nose at the smells emanating from the nested use of the IDocumentSession (db) and the need for multiple Load calls to fill the Mentees List.

How can I optimise this method using best practice RavenDB syntax?

Edit
Thanks to @Jonah Himango (see accepted answer below) who solved the problem of multiple calls to the database for me. In addition I have also created a new extension method called ‘Memoize’ to eliminate the need for the external ‘mentees’ result List (see code above).

Here is the optimised code. Please feel free to comment and refine further.

The Linq

public static List<User> GetMentees(IDocumentSession db, string mentorId)
{
    return  db.Query<Relationship>()
            .Customize(x => x.Include<Relationship>(o => o.MenteeId))
            .Where(r => r.MentorId == mentorId)
            .Memoize()
            .Select(r => db.Load<User>(r.MenteeId))
            .ToList();
}

The extension method

public static List<T> Memoize<T>(this IQueryable<T> target)
{
    return target.ToList();
}

Note : This extension method may seem completely superfluous (it is really) but it irritates my geek-gland that I have to call a function called ToList(), not to create a list, but to force the execution of the Linq statement. So my extension method just renames ToList() to the far more accurate Memoize().

  • 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-11T02:34:39+00:00Added an answer on June 11, 2026 at 2:34 am

    You’ll want to use the .Include query customization to tell Raven to include the related user object off of each Relationship:

    db.Query<Relationship>()
                .Customize(x => x.Include<Relationship>(o => o.MenteeId))
                .Where(r => r.MentorId == mentorId)
                .Select(r => r.MenteeId)
                .ForEach(id => mentees.Add(db.Load<User>(id))); // .Load no longer makes a call to the DB; it's already loaded into the session!
    

    Relevant documentation here:

    The call to Load() is resolved completely client side (i.e. without
    additional requests to the RavenDB server) because the [related]
    object has already been retrieved via the .Include call.

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

Sidebar

Related Questions

I have an object called users that I use to store user objects that
My User object that I want to create and store in the datastore has
I have two objects a user and a role which I make persistant using
I have a User object which has a collection of Transaction objects under it.
Say I have a collection of Users. Each user has a User_ID , Username
I have a List of User objects from my data store. Now once it
I have a C# class to store my User details and another for storing
I'm using RavenDB & need to store dynamic data like: public class User {
I have simple set of objects stored in RavenDB: public class Question { public
User Story: Action for Facebook that has open graph object. For this I need

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.