如何在 IQueryable 中使用带有列表整数的过滤结果
How to use filter result with list integer in IQueryable
我的程序包含几个 table。其中一些 table 如下:
国家 table - 产品 table - ProductCountry(交界处 table)。
当在商店页面上应用过滤器时(示例。com/shop),我想将所选国家/地区的过滤器应用于结果,它经历了各个阶段(这些国家/地区的 ID 是整数列表)
写了好几种方式,都有错误。
感谢您的帮助
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)));
}
尝试以下过滤器:
result = result.Where(c =>
c.Product.CountryProducts.Any(b => countryId.Contains(b.CountryId))
);
我的程序包含几个 table。其中一些 table 如下: 国家 table - 产品 table - ProductCountry(交界处 table)。 当在商店页面上应用过滤器时(示例。com/shop),我想将所选国家/地区的过滤器应用于结果,它经历了各个阶段(这些国家/地区的 ID 是整数列表) 写了好几种方式,都有错误。 感谢您的帮助
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)));
}
尝试以下过滤器:
result = result.Where(c =>
c.Product.CountryProducts.Any(b => countryId.Contains(b.CountryId))
);