Entity Framework Generic repository including properties through parameter

后端 未结 3 1772
日久生厌
日久生厌 2020-12-20 06:59

I have an implementation of a Generic Repository in Entity Framework which I am trying to improve to use the .Include(..) function provided by EF instead of including the na

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 07:25

    protected internal IQueryable Filter(Expression> predicate, params Expression>[] includeProperties)
    {
        var query = RetrieveQuery();
    
        if (predicate != null)
        {
            query = query.Where(predicate).AsQueryable();
        }
    
        if (includeProperties != null)
        {
            query = _queryableUnitOfWork.ApplyIncludesOnQuery(query, includeProperties);
        }
    
        return (query);
    }
    

    And this method called there

    public IQueryable ApplyIncludesOnQuery(IQueryable query, params Expression>[] includeProperties) where TEntity : class, IEntity
    {
        // Return Applied Includes query
        return (includeProperties.Aggregate(query, (current, include) => current.Include(include)));
    }
    

    Filter Method Call

     public IEnumerable GetActiveShowStockProductListByProduct(int productId)
                {
                    var foundedPStockroducts = Filter(
                        ent => ent.ProductId == productId && ent.IsActive,
                        ent => ent.StockProductPrices,
                        ent => ent.StockProductDepots,
                        ent => ent.StockProductSpecificationValues,
                        ent => ent.StockProductSpecificationValues.Select(spsv => spsv.SpecificationValue)
                        );
    
                    // Map foundedPStockroducts to showStockProductList
                    var showStockProductList = TypeAdapterFactory.Adapter.Adapt>(foundedPStockroducts).ToList();
    
                    return (showStockProductList);
                }
    

提交回复
热议问题