简体   繁体   English

类别 - 带有 dapper 和 SQL Server 的子类别列表

[英]Category - subcategory listing with dapper and SQL Server

I have a requirement where there are two entities: Category and Bookmark .我有一个要求,其中有两个实体: CategoryBookmark A category can contain some subcategories and/or some bookmarks.一个类别可以包含一些子类别和/或一些书签。 My table structure is as shown below:我的表结构如下图所示:

tblCategories tblCategories

ID int PK,
Name nvarchar

tblBookmarks tbl书签

ID int PK
Name nvarchar

tblBookmarkCategories tblBookmarkCategories

CategoryID int (foreignKey)
BookmarkID int (foreignKey)

Similarly my entities are as following:同样,我的实体如下:

public class CategoryTree
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<CategoryTree> SubCategory { get; set; }
    public ICollection<Bookmark> Bookmarks { get; set; }
}

public class Bookmark
{
    public int ID { get; set; }
    public string Name { get; set; }
}

How can I show the category list along with their subcategories and bookmarks using dapper?如何使用 dapper 显示类别列表及其子类别和书签? I want to display the result in the following format:我想以以下格式显示结果:

Category 1
   Bookmark 1
   Category 1.1
       Bookmark 1.1
       Bookmark 1.2
   Category 1.2
Category 2
   Bookmark 2
   Category 2.1
       Category 2.1.1
   Category 2.2

Since the Category and Subcategory also contain relationship, you could add a "ParentID" column in the "tblCategories" table, by using it, you could find the subcategory based on the "ParentID" column, like this:由于类别和子类别也包含关系,您可以在“tblCategories”表中添加一个“ParentID”列,通过使用它,您可以根据“ParentID”列找到子类别,如下所示:

在此处输入图片说明

The Dapper is used to query data from the database, I prefer to load all the categories and the bookmarks at once, then based on the category to find the subcategories and bookmarks. Dapper用于从数据库中查询数据,我更喜欢一次加载所有类别和书签,然后根据类别查找子类别和书签。

So, I create the following classes:所以,我创建了以下类:

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int ParentID { get; set; }

    public virtual ICollection<Category> SubCategories { get; set; }
    public virtual ICollection<Bookmark> Bookmarks { get; set; }
}

public class Bookmark {
    public int ID { get; set; }
    public string Name { get; set; }
    public int CategoryID { get; set; }
}

To get/display the category and its subcategory, we could use the recursive method to check whether current category contains subcategory, if it contains the subcategory, filter the data from the category list.要获取/显示类别及其子类别,我们可以使用递归方法检查当前类别是否包含子类别,如果包含子类别,则从类别列表中过滤数据。 The detail code as below:详细代码如下:

    public class HomeController : Controller
    { 
        private readonly IConfiguration _configuration; 
        public HomeController(IConfiguration configuration)
        { 
            _configuration = configuration;
        } 
        //Index page, display the categories and bookmarks
        public IActionResult DapperIndex()
        {
            //get the connection string
            var connectionstring = _configuration.GetConnectionString("DefaultConnection");

            //get all categories and bookmarks
            List<Category> allcategories = new List<Category>();
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                allcategories = connection.Query<Category>("SELECT * FROM tblCategories").ToList(); 

            }
            List<Bookmark> allbookmarks = new List<Bookmark>();
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                allbookmarks = connection.Query<Bookmark>("select b.Id, b.Name, bc.CategoryID from tblBookmarks as b join tblBookmarkCategories as bc on b.id = bc.BookmarkID").ToList();

            }
            List<Category> categories = allcategories
                .Where(e => e.ParentID == 0) /* grab only the root parent nodes */
                .Select(e => new Category
                {
                    ID = e.ID,
                    Name = e.Name,
                    ParentID = e.ParentID,
                    SubCategories = GetSubCategory(allcategories,allbookmarks, e.ID), /* Recursively grab the children */
                    Bookmarks = GetBookmarks(allbookmarks, e.ID)
                }).ToList();

            ViewBag.categoriesList = categories;
            return View();
        }
        //get the subcategories
        private static List<Category> GetSubCategory(List<Category> items, List<Bookmark> bookmarks, int parentId)
        {
            return items.Where(x => x.ParentID == parentId)
                .Select(e => new Category
                {
                    ID = e.ID,
                    Name = e.Name,
                    ParentID = e.ParentID,
                    SubCategories = GetSubCategory(items,bookmarks, e.ID),
                    Bookmarks = GetBookmarks(bookmarks,e.ID) 
                }).ToList();
        }
        //get the bookmarks.
        public static List<Bookmark> GetBookmarks(List<Bookmark> bookmarks, int categoryid)
        {
            return bookmarks.Where(c => c.CategoryID == categoryid).Select(e=> new Bookmark()
            {
                ID= e.ID,
                Name = e.Name,
                CategoryID = e.CategoryID
            }).ToList();
        } 
    }

Code in the .cshtml page: .cshtml 页面中的代码:

    @{ 
        List<Category> categorylist = ViewBag.categoriesList as List<Category>;
        void ShowTree(List<Category> categories)
        {
            if (categories != null)
            {
                foreach (var item in categories)
                {
                    <li>
                        <span>@item.Name</span>
                        @*display the bookmarks*@
                        @if (item.Bookmarks.Any())
                        {
                            foreach (var mark in item.Bookmarks)
                            {

                                <ul> @mark.Name  </ul>
                            }
                        }
                        @*display the subcategories*@
                        @if (item.SubCategories.Any())
                        {
                            <ul>
                                @{ ShowTree(item.SubCategories.ToList());}
                            </ul>
                        }
                    </li>
                }
            }
        }
        ShowTree(categorylist);
    }

The result as below:结果如下:

在此处输入图片说明

Edit: To use Dapper in Asp.net Core application, install "Dapper" via NuGet, then check the Dapper Query Basics .编辑:要在 Asp.net Core 应用程序中使用 Dapper,请通过 NuGet 安装“Dapper”,然后查看Dapper Query Basics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM