How to filter and include children using Z.EntityFramework

entity-framework entity-framework-6 entity-framework-plus

Question

The query below is using Z.EntityFramework.Plus.EF6 to filter children. Its filtering and including the AssetXref entity correctly however the result is not able to include Child.Parent entity relationship

   var result = await _repository.GetQuery<Assets>()
                      .IncludeFilter(x => x.AssetsXRef
                                        .Where(y => y.Child.Perent.ParentID == parentID)
                                        .Select(y => y.Child.Perent)
                                    )
                     .Where(x => x.Active == true)
                     .ToListAsync();

I have also tried

   var result = await _repository.GetQuery<Assets>()
                      .IncludeFilter(x => x.AssetsXRef
                                        .Where(y => y.Child.Perent.ParentID == parentID)                                        
                                    )
                     .Include(x=>x.AssetsXRef.Select(y=>y.Child.Parent))
                     .Where(x => x.Active == true)
                     .ToListAsync();
1
0
9/20/2019 6:02:07 PM

Expert Answer

The IncludeFilter is not compatible with Include. You need to use IncludeFilter all the way even if there is no filter

In addition, you need to always use the full parent filter on child

Here is an example:

var result = await _repository.GetQuery<Assets>()
                     .IncludeFilter(x => x.AssetsXRef.Where(y => y.Child.Perent.ParentID == parentID))
                     // Might be SelectMany
                     .IncludeFilter(x => x.AssetsXRef.Where(y => y.Child.Perent.ParentID == parentID).Select(y=>y.Child.Parent))
                     .Where(x => x.Active == true)
                     .ToListAsync();
0
9/27/2019 10:47:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow