question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested Includes, how to query?

See original GitHub issue

This was apparently done in 3.x, yet I haven’t found an example of it in the Wiki or any of the issues.

public class Book
{
    [BsonId] public string Id { get; set; }
    public List<Author> Authors { get; set; }
}

public class Author
{
    public string Role { get; set; }
    [BsonRef("users")] public User User { get; set; }
}

public class User 
{
    [BsonId] public string Id { get; set; }
    public string Name { get; set; }
}

I want to query books and get the full User object. How is this possible? I’ve tried:

  1. books.Include(x => x.Authors[0].User).FindById(id);
  2. books.Include("Authors.User").FindById(id);

Both return only the user’s Id (i.e. no inclusion). Any hints or suggestions would be appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mbdavidcommented, Apr 3, 2017

Hi @freezy, there is no nested includes for List. In this case it´s better use Include(Action<T>), so you can define your function, likes:

books.Include(x => x.Authors.ForEach(z => z.User = users.FindById(z.User.UserId))

0reactions
ghostcommented, Jan 12, 2019

Hy, I try to achieve the same thing, but I didn’t get it to work. I already tried the example above with the suggested function (but I guess it should be z.User.Id instead of z.User.UserId): books.Include(x => x.Authors.ForEach(z => z.User = users.FindById(z.User.UserId))

But I get the error message (Cannot convert lambda expression to ‘string’ because it’s not a delegate type)

Is this sample still valid with the version 4.1.4.0?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails - Nested includes on Active Records?
I'm trying to include every user associated to this event and every profile associated to each user. The Users get included but not...
Read more >
How to create a nested include in Sequelize query
Once the associations are created, you can query the data from the three tables by adding a nested include option into your query...
Read more >
Rails includes for nested active-record associations
If you want to include an association (we'll call it C) of an already included association (we'll call it B), you'd use the...
Read more >
Nested includes and joins - Space Vatican
There are 3 rules for nested :includes: - Always use the association name. Not the table name, not the class name but the...
Read more >
Relation queries (Concepts) - Prisma
A key feature of Prisma Client is the ability to query relations between two or more models. Relation queries include: Nested reads (sometimes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found