0

I'm working on a screen where I'm displaying the list of products but when the user clicks on different checkboxes, I got the value of checked checkboxes value in action parameter. Here is my screen.

Product Screen

Now what I want to request the query dynamically on different items selection.

   public async Task<IEnumerable<Product>> GetSpecificProducts
        (List<int> brands, List<int> categories, List<int> sugar)
    {
        var products = await _context.Products
            .Where(x => x.BrandId == ???)
            .Where(x => x.CategoryId == ???)
            .Where(x => x.Sugar == ???)
            .ToListAsync();
    }`

I've already take a look on some stackoverflow questions but I'm just getting results in case of single value like here. How can I generate the dynamic linq on multiple where clauses.

3
  • use where(x => list.contains(x.someId)) Commented Jun 22, 2018 at 7:53
  • One more thing please, sugar is my boolean property how can I deal with it, prntscr.com/jxxt0e Commented Jun 22, 2018 at 8:08
  • @UsamaShahid so why did you pass a List<int> sugar if Sugar is a boolean property? Instead of List<int> sugar it should be bool sugar in your parameters method. Commented Jun 22, 2018 at 8:12

2 Answers 2

3

You just need to check that each collection is not empty before including it as a filter of your query like below:

public async Task<IEnumerable<Product>> GetSpecificProducts 
     (List<int> brands, List<int> categories, bool? sugar)
{
    IQueryable<Product> query = _context.Products;

    if(brands != null && brands.Any()) 
        query = query.Where(x => brands.Contains(x.BrandId));

    if(categories != null && categories.Any()) 
        query = query.Where(x => categories.Contains(x.CategoryId));

    if(sugar.HasValue) 
        query = query.Where(x => x.Sugar == sugar);

    var products = await query.ToListAsync();
}
Sign up to request clarification or add additional context in comments.

13 Comments

One more thing please, sugar is my boolean property in the database. I'm setting the values against sugar and non sugar in this way, prnt.sc/jxxt0e. Would you please guide me what can I do it in this scenario.
@UsamaShahid I edited my answer below. You should pass sugar as a bool? and set its value only when the user selected it.
@UsamaShahid and when the user selects Sugar and NonSugar you let the value null
@UsamaShahid I haven't access to this page. It is blocked :) Can you add the error as a comment please.
Ooops! it was my mistake. I was missing to assign it again in query variable. query = query.Where(x => x.Sugar == sugar) Thank you so much @CodeNotFound
|
0

when you want a list of Product where it's brandId is in the list of brands, categoryId is in list of categorise and sugar is in list of sugar, you can do it like.

public async Task<IEnumerable<Product>> GetSpecificProducts
    (List<int> brands, List<int> categories, List<int> sugar)
{
    var products = await _context.Products
        .Where(x => brands.Contains(x.BrandId) &&
                    categories.Contains(x.CategoryId) &&
                    sugar.Contains(x.Sugar))
        .ToListAsync();
}

----------Update----------

as you have mentioned that sugar is bool property in your database, and as you are already suggested you can change your method's signature.

Also updating my code to check emptyness of lists, if lists are empty that means user has not selected any filter and thus we will ignore that criteria in filtering.

public async Task<IEnumerable<Product>> GetSpecificProducts
    (List<int> brands, List<int> categories, bool? sugar)
{
    var products = await _context.Products
        .Where(x => (brands.Any() ? brands.Contains(x.BrandId) : true) &&
                    (categories.Any() ? categories.Contains(x.CategoryId) : true) &&
                    (sugar.HasValue ? sugar == x.Sugar : true))
        .ToListAsync();
}

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.