Home > OS >  How to use filter result with list integer in IQueryable
How to use filter result with list integer in IQueryable

Time:04-19

My program contains several tables. Some of these tables are as follows: Country table - Product table - ProductCountry (junction table). When filters are applied on the page of the store (example.com/shop), I want to apply the filter of the selected countries to the result, which goes through various stages (the ID of these countries is a list of integer) I wrote several ways, all of which have errors. Thank you for your help


IQueryable<ProductLocalization> result = _context.ProductLocalizations
                .Include(c => c.Languages)
                .Include(c => c.ProductPropertyLocalizations)
                .Include(c => c.Product)
                .Include(c => c.Product.UserProducts)
                .Include(c => c.Product.Brand)
                .Include(c => c.Product.Category)
                .Include(c => c.Product.Category.CategoryPropertyLocalizations)
                .Where(c => c.LanguageId == languageId);

if (countryId.Count != 0)
            {
                result = result.Where(c => countryId.Contains(c.Product.CountryProducts.Select(b => b.CountryId));
                result = result.Where(c => c.Product.CountryProducts.Any(b => b.CountryId == countryId));
                result = result.Where(c => countryId.Any(b => b == c.Product.CountryProducts[0].CountryId)); //for example
                result = result.Where(c => c.Product.CountryProducts.Select(c => countryId.Any(c.CountryId)));
                result = result.Where(c => c.Product.CountryProducts.FindAll(b => b.CountryId == countryId));
                result = result.Where(c => c.Product.CountryProducts.Select(c => c.CountryId).Contains(countryId);
                result = result.Where(c => countryId.Any(b => b == c.Product.CountryProducts.Select(a=>a.CountryId)));
            }

CodePudding user response:

Try the following filter:

result = result.Where(c => 
   c.Product.CountryProducts.Any(b => countryId.Contains(b.CountryId))
);
  • Related