Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda expression for multiple Include #41

Closed
ovation22 opened this issue Aug 14, 2020 · 5 comments
Closed

Lambda expression for multiple Include #41

ovation22 opened this issue Aug 14, 2020 · 5 comments

Comments

@ovation22
Copy link

Hi Steve,

I appreciate all the work on this that you (and community) have put into this.

Is it possible to have multiple includes using expression syntax? I was able to chain includes using string then expression, but would really like to use the typed expressions if possible.

Expected behavior:

Query.Include(x => x.foo).Include(x => x.bar)

should compile and produce the same results as:

Query.Include("foo").Include(x => x.bar)

I'm happy to look into what it would take and will try to spend some time with it this weekend.

@ovation22 ovation22 changed the title Lambda expression for multiple Include - Query.Include(x => x.foo).Include(x => x.bar) Lambda expression for multiple Include Aug 14, 2020
@fiseni
Copy link
Collaborator

fiseni commented Aug 14, 2020

Hi @ovation22,

Sure, you can do that. Just start with Query once again. In case of Include and Order functionalities, once you enter the second level, you can use only "Then" definitions, so you have to start over with Query.

Query.Include(x => x.foo);
Query.Include(x => x.bar);

It doesn't matter in which order you define the statements, or if you use chaining, or if you writing everything in separate statements. Whatever you add through the builder (named as Query), will be evaluated in predefined order.

Examples:

Query.Where(x => x.Name == "Something")
     .Paginate(10, 20)
     .Include(x => x.SomeProperty1)
         .ThenInclude(x => x.SecondLevelProperty1)
         .ThenInclude(x => x.ThirdLevelProperty1);
Query.Include(x => x.SomeProperty2);

The same can be written with all separate statements:

Query.Where(x => x.Name == "Something")
Query.Paginate(10, 20)
Query.Include(x => x.SomeProperty1)
          .ThenInclude(x => x.SecondLevelProperty1)
          .ThenInclude(x => x.ThirdLevelProperty1);
Query.Include(x => x.SomeProperty2);
Query.OrderBy(x => x.SomeProperty1)
         .ThenByDescending(x => x.SomeProperty2)
         .ThenBy(x => x.SomeProperty3;

@ovation22
Copy link
Author

Ah, ok, that makes sense. Thank you so much!

@ShadyNagy
Copy link
Contributor

Hello @fiseni
is that possible? SecondLevelProperty1 and SecondLevelProperty2 from SomeProperty1 can get by this way or there is another way to get it?

Query.Where(x => x.Name == "Something")
     .Paginate(10, 20)
     .Include(x => x.SomeProperty1)
         .ThenInclude(x => x.SecondLevelProperty1)
     .Include(x => x.SomeProperty1)
         .ThenInclude(x => x.SecondLevelProperty2);

@fiseni
Copy link
Collaborator

fiseni commented Jul 11, 2022

Hey @ShadyNagy

Yes, that's the correct way of doing it. Even when working with EF directly, you still have to do it that way, starting from the main navigation once again.

PS. This issue is old, At that time we couldn't use Include after ThenInclude and you had to start with Query again. But, we fixed that (I think since version 6.0) and your code works now. Now it looks the same as you would have written it with EF directly.

@ShadyNagy
Copy link
Contributor

@fiseni Thanks so much
I will try to see how the EF will translate this code to SQL :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants