获取所选主类别的所有子类别中的所有产品的最佳方法是什么?试图在大约 80 个子类别中获取 10000 个产品,但这种方法太慢了。有什么建议么?使用 c# 和 Linq to SQL
//list for the child categories
private List<int> catsChildList = new List<int>();
GetChildCats(123);
//get all the child categories of main category '123'
private void GetChildCats(int _parentCat)
{
var cats = (from c in db2.tbl_cart_categories
where c.ParentID == _parentCat
select new { c.CategoryID });
if (cats.Count() > 0)
{
foreach (var cat in cats)
{
int _cat = Convert.ToInt32(cat.CategoryID);
catsChildList.Add(_cat);
GetChildCats(_cat);
}
}
}
//Get the products
var products = (from p in db2.products_infos
where p.IsEnabled == true
group p by p.ProdID
into g where g.Any(x => catsChildList.Contains(Convert.ToInt32(x.CategoryID)))
返回结果大约需要 6 秒