0

I need to have a method like this, where I can apply Where(x =>x. ...) and Include(x => x.RelatedEntity) and OrderBy(x=>x. ...) on a given entity.

Something like this:

public List<TEntity> ApplyFilter<TEntity>(TEntity entity,
                                          List<filters> filters /* List of filters: 'filters' */)
                                          where TEntity : BaseEntity
    {
        using (var db = new MyDbContext()){
        var query = db.Set<TEntity>().AsQueryable;
   //apply filters to 'query'
        query.include(/*multiple related entities*/);
        query.applyfilters(/*filters*/);

        return query.ToList();
    }
}

And I need to pass what I need to be filtered/included as lambda expressions.

NOTE: I searched a lot about how I can do it but I really wasn't able to find anything. I'm new to this part of C# / Entity Framework and I really didn't even know what keywords to search for.

Thank you for the help

2 Answers 2

1

You'll want to use a LINQ expression

    public List<TEntity> ApplyFilter<TEntity>(            
        Expression<Func<TEntity, bool>> filter,
        Expression<Func<TEntity, object>> orderBy,
        params Expression<Func<TEntity, object>>[] includes) where TEntity : BaseEntity
    {
        using (var db = new MyDbContext())
        {
            var query = db.Set<TEntity>().AsQueryable();
            query = query.Where(filter);
            query = query.OrderBy(orderBy);

            if (includes != null)
            {
                foreach (var include in includes)
                {
                    query = query.Include(include);
                }
            }

            return query.ToList();
        }
    }

To use the method:

        ApplyFilter<TestObject>(
            x => x.Prop1 == "foo", 
            x => x.Prop2,
            x => x.Prop3, x => x.Prop4);
Sign up to request clarification or add additional context in comments.

1 Comment

Some of this is accurate, expressions, using the filter in where for example, however, orderby must use something else in order to work. The type needs to be Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderby and the use needs to be query = orderby(query).
0

Like this?

    var result = Repository.PurchaseProposalItem.GetDbSet();

        if (filters.FilterByBrand) result = result.Where(p => p.GS_Product.GS_ProductBrand.PBr_Id == filters.BrandId);
        if (filters.FilterByFamily) result = result.Where(p => p.GS_Product.GS_ProductFamily.PFa_Id == filters.FamilyId);
        if (filters.FilterBySubFamily) result = result.Where(p => p.GS_Product.GS_ProductSubFamily.PSu_Id == filters.SubFamilyId);
        if (filters.FilterByProductType) result = result.Where(p => p.GS_Product.Pro_Type == filters.ProductTypeEnum);

    return result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.