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

EF Core ignores 'Include' method after using 'SelectMany' #14667

Closed
AlexandrTsvetkov opened this issue Feb 10, 2019 · 2 comments
Closed

EF Core ignores 'Include' method after using 'SelectMany' #14667

AlexandrTsvetkov opened this issue Feb 10, 2019 · 2 comments

Comments

@AlexandrTsvetkov
Copy link

It seems, EF Core ignores Include(...) method after calling SelectMany(...)

Steps to reproduce

Let's say I have 4 basic models:

    public class Order
    {
        public int Id { get; set; }

        public IEnumerable<OrderItem> Items { get; set; }
    }

    public class OrderItem
    {
        public int Id { get; set; }

        public int ProductId { get; set; }

        public Product Product { get; set; }

        public int OrderId { get; set; }

        public Order Order { get; set; }
    }

    public class Product
    {
        public int Id { get; set; }

        public int UserId { get; set; }

        public User User { get; set; }
    }

    public class User
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

And DbContext

    public class AppDbContext :  DbContext
    {
        public DbSet<User> Users { get; set; }

        public DbSet<Order> Orders { get; set; }

        public DbSet<OrderItem> OrderItems { get; set; }

        public DbSet<Product> Products { get; set; }

        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {
        }
    }

My case is I am trying to get all Products with Users from all Orders. Basically, I have two ways how to do it, but first way ignores Include and second doesn't.

  1. Ingoring Include
context.Orders.SelectMany(x => x.Items.Select(y => y.Product))
                .Include(x => x.User).ToList();

Output SQL:

SELECT [x.Items.Product].[Id], [x.Items.Product].[UserId]
FROM [Orders] AS [x]
INNER JOIN [OrderItems] AS [x.Items] ON [x].[Id] = [x.Items].[OrderId]
INNER JOIN [Products] AS [x.Items.Product] ON [x.Items].[ProductId] = [x.Items.Product].[Id]
  1. Not ignoring Include
context.Orders.SelectMany(x => x.Items).Select(y => y.Product)
                .Include(x => x.User).ToList();

Output SQL:

SELECT [x.Items.Product].[Id], [x.Items.Product].[UserId], [x.Items.Product.User].[Id], [x.Items.Product.User].[Name]
FROM [Orders] AS [x]
INNER JOIN [OrderItems] AS [x.Items] ON [x].[Id] = [x.Items].[OrderId]
INNER JOIN [Products] AS [x.Items.Product] ON [x.Items].[ProductId] = [x.Items.Product].[Id]
INNER JOIN [Users] AS [x.Items.Product.User] ON [x.Items.Product].[UserId] = [x.Items.Product.User].[Id]

Further technical details

EF Core version: 2.2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.6

@ajcvickers
Copy link
Contributor

@smitpatel to find dupe.

@smitpatel
Copy link
Contributor

Duplicate of #12737

@smitpatel smitpatel marked this as a duplicate of #12737 Feb 11, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants