Entity Framework 4.1 - Select

旧时模样 提交于 2019-12-11 02:42:23

问题


I am using the following expressions:

ProductRepository.Query.Include(Function(x) x.ChildProducts.Select(Function(y) y.PriceTiers.Where(Function(z) z.IsActive))).Where(Function(x) x.Categories.Any(Function(y) y.ID = ID))

And getting this error:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

If I remove this:

.Where(Function(z) z.IsActive)

It works fine, but I need to filter the inactive price tiers.

Any ideas?

Update

Here is my solution:

Public Function GetProductsByCategoryID(ID As Integer) As System.Collections.Generic.IEnumerable(Of Core.Entities.Product) Implements Core.Interfaces.IProductService.GetProductsByCategoryID
        Dim Col = ProductRepository.Query.Where(Function(x) x.Display AndAlso x.Categories.Any(Function(y) y.ID = ID)) _
                  .Select(Function(x) New With { _
                              .Product = x,
                              .ChildProducts = x.ChildProducts.Where(Function(y) y.Display).Select(Function(y) New With { _
                                                                                                       .ChildProduct = y,
                                                                                                       .PriceTiers = y.PriceTiers.Where(Function(z) z.IsActive)
                                                                                                   })
                          }).ToList.Select(Function(x) x.Product)

        Return Col

    End Function

回答1:


You can't do that in single query without projection or separate query to load the navigation property. Lambda expression for Include accepts only dotted notation for references and Select for collections but you can't do any filtering.

Filtering is possible on in separate query when using DbContext API:

var data = context.Entry(product)
                  .Collection(p => p.ChildProducts)
                  .Query()
                  // Here are your filters
                  .Load();

Edit:

Projection requires something like:

var data = context.Products
                  .Where(...)
                  .Select(p => new 
                      {
                          Product = p,
                          ChildProducts = p.ChildProducts.Where(...)
                      });


来源:https://stackoverflow.com/questions/5658219/entity-framework-4-1-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!