0

产品表: ID名称...

类别表: ID ParentCategoryID 名称

ProductCategories 表: ID CatID ProdID

和.. CatID 外键和 ProdID 外键中的 ProductCategories

多个 SQL 查询而不是单个查询。如何查询? List<NameIDCountList> NameIDCountList =林克?

public class NameIDCount
{
    public int ID {get;set;}
    public string Name { get;set;}
    public int Count { get;set;}
}

public class NameIDCountList
    {
        public NameIDCount _Category { get; set; }
        public List<NameIDCount> _SubCategories { get; set; }
    }

我的方法:

public List<NameIDCountList> getCategories(int CatID)
            {


            List<NameIDCountList> CategoryModel = new List<NameIDCountList>();

            List<NameIDCount> MainCats = (from k in data.Categories.Where(p => p.ParentCategoryID == CatID && p.ID != 0 && p.isActive).Select(p => new NameAndID{ Name = p.Name, ID = p.ID}).ToList()
                                                                   join uk in data.ProductCategoriries
                                                                   on k.ID equals uk.catID
                                                                   join u in Product
                                                                   on uk.ProdID equals u.ID
                                                                   group u by k into grp
                                                                   orderby grp.Key.AD
                                                                   select new NameIDCount
                                                                   {
                                                                       Name = grp.Key.Name,
                                                                       ID = grp.Key.ID,
                                                                       _COUNT = grp.Count()
                                                                   }).ToList();


            foreach (var cat in MainCats)
                        {
                            NameIDCountList cm =
                                new NameIDCountList()
                                {
                                    __Category = cat,
                                    _SubCategories = (from k in data.Categories.Where(p => p.ParentCategoryID == cat.ID && p.isActive).Select(p => new NameAndID{ Name = p.Name, ID =p.ID}).ToList()
                                                                   join uk in data.ProductCategoriries
                                                                   on k.ID equals uk.catID
                                                                   join u in Product
                                                                   on uk.ProdID equals u.ID
                                                                   group u by k into grp
                                                                   orderby grp.Key.AD
                                                                   select new NameIDCount
                                                                   {
                                                                       Name = grp.Key.Name,
                                                                       ID = grp.Key.ID,
                                                                       _COUNT = grp.Count()
                                                                   }).ToList();
                                };
                            CategoryModel.Add(cm);
                        }
                return CategoryModel;
            }

结果:

<a href="/category/id=1">PC [23]</a>
    <a href="/category/id=4">NoteBook [2]</a>
    <a href="/category/id=5">Desktop [21]</a>
<a href="/category/id=2">Monitor [6]</a>
     <a href="/category/id=8">LED Monitor [4]</a>
     <a href="/category/id=9">LCD Monitor [2]</a>
4

0 回答 0