我的程式包含幾個表。其中一些表如下: Country 表 - Product 表 - ProductCountry(連接表)。當在商店頁面(example.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)));
}
uj5u.com熱心網友回復:
嘗試以下過濾器:
result = result.Where(c =>
c.Product.CountryProducts.Any(b => countryId.Contains(b.CountryId))
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459594.html
標籤:实体框架 林克 实体框架核心 ef-core-3.1
上一篇:物體框架更新多對多關系的中間表
