开发者

Entity Framework 4.1 - Select

开发者 https://www.devze.com 2023-02-24 22:31 出处:网络
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.Ca

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


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(...)
                      });
0

精彩评论

暂无评论...
验证码 换一张
取 消