opened
orobert91
username_0
Given the following query:

```
var inventorySummary = myContext.InventoryEntries
    .Include(ie => ie.PurchaseOrderItem).ThenInclude(poi => poi.PurchaseOrder)
    .Where(ie => new HashSet<Guid>() { new Guid("cdd40f38-d3ff-4d2f-be66-ace7f2090745") }.Contains(ie.PurchaseOrderItem.PurchaseOrderId.Value))
    .GroupBy(ie => new { ie.Item, ie.Warehouse })
    .Select(g => new InventorySummaryEntry() {
        Entries = g.ToList()
    })
    .ToListAsync();
```

One would expect that` Entries` contains` InventoryEntry` with their included` PurchaseOrderItem`, but they do not:

Assert.NotNull(inventorySummary.Entries.First().PurchaseOrderItem) //Fails

- Removing `GroupBy()` and `Select()` clause produces the expected result
- Keeping `GroupBy(...)` but replacing `Where(...)` by simple equality (`.Where(ie => ie == new Guid("cdd40f38-d3ff-4d2f-be66-ace7f2090745")`) also produces the expected result.
Include() not working when filtered with Contains() and aggregating to list
issue
created
ajcvickers
username_1
@username_2 to reference in 3.0 Include issue and respond to customer.
comment
created
smitpatel
username_2
```C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EFSampleApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var db = new MyContext())
            {
                // Recreate database
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                // Seed database
                db.Add(
                    new InventoryEntry
                    {
                        Item = 1,
                        Warehouse = 1,
                        PurchaseOrderItem = new PurchaseOrderItem
                        {
                            PurchaseOrder = new PurchaseOrder
                            {
                                Id = new Guid("cdd40f38-d3ff-4d2f-be66-ace7f2090745")
                            }
                        }
                    }
                    );

                db.SaveChanges();
            }

            using (var db = new MyContext())
            {
                // Run queries
                var inventorySummary = db.InventoryEntries
                    .Include(ie => ie.PurchaseOrderItem)
                        .ThenInclude(poi => poi.PurchaseOrder)
                    .Where(ie => new HashSet<Guid>()
                        {
                            new Guid("cdd40f38-d3ff-4d2f-be66-ace7f2090745")
                        }
                        .Contains(ie.PurchaseOrderItem.PurchaseOrderId.Value))
                    .GroupBy(ie => new { ie.Item, ie.Warehouse })
                    .Select(g => new
                    {
                        Entries = g//.ToList()
                    })
                    .ToList();

                Console.WriteLine(inventorySummary[0].Entries.First().PurchaseOrderItem.PurchaseOrder);
            }

            Console.WriteLine("Program finished.");
        }
    }


    public class MyContext : DbContext
    {
        private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);

        // Declare DBSets
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<InventoryEntry> InventoryEntries { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Select 1 provider
            optionsBuilder
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
                //.UseSqlite("filename=_modelApp.db")
                //.UseInMemoryDatabase(databaseName: "_modelApp")
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(LoggerFactory);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Configure model
        }
    }

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

    public class InventoryEntry
    {
        public int Id { get; set; }
        public int Item { get; set; }
        public int Warehouse { get; set; }
        public PurchaseOrderItem PurchaseOrderItem { get; set; }
    }

    public class PurchaseOrderItem
    {
        public int Id { get; set; }
        public Guid? PurchaseOrderId { get; set; }
        public PurchaseOrder PurchaseOrder { get; set; }
    }

    public class PurchaseOrder
    {
        public Guid Id { get; set; }
    }
}
```

Apparently we fail to identify where to put Include in QueryModel.
comment
created
smitpatel
username_2
Related #12795
comment
created
Menighin
username_3
Just hit a similar problem. On the following query, the included `ScheduledStopActivity` and `ScheduledStopActivityTranslations` are `null` if I filter with the `Contains`. If not, they are defined:

    var entities = _context.DbScheduledStop.Include(o => o.ScheduledStopActivity).ThenInclude(s => s.ScheduledStopActivityTranslations)
                     .Include(o => o.Vehicle).Include(o => o.Location)
                     .Where(o => idList.Contains(o.TrainId))
                     .ToList();

Checking the query, there is no join for those entities.

Is it the same problem? Is there a workaround for this?
comment
created
smitpatel
username_2
@username_3 - Can you file a new issue with repro code? Based on initial investigation done on this issue, it was caused because of GroupBy + Contains combination. Since you query does not contain GroupBy, it may be similar issue but with different root cause.
comment
created
dracan
username_4
I've just hit the same issue. I'm using `GroupJoin`. Works as expected if my query doesn't include `Contains`, however if I use contains, then navigation property is null.
comment
closed
smitpatel
username_2
issue
created
smitpatel
username_2
Won't fix as per #17068
comment
created
kot-pilot
username_5
i have the same situation, include is ignored when filtering with 'contains' of some array/list
like 

var entities=context.Items.Include(x=>x.SubItem).Where(x => statuses.Contains(x.Status)).ToList()

Maybe it's already fixed? Or some workaround for that?
comment
created
zuckerthoben
username_6
Did you find a solution for the Include with contains problem? @username_5 @username_4 @username_3
comment