.net 从数据库填充嵌套集合

.net 从数据库填充嵌套集合,.net,vb.net,.net,Vb.net,我有以下三门课 Book Product SpecialOptions 书很多,每本书都有很多产品。同样,在一种产品中也有许多不同的品种。这三个类中的每一个都有其他属性,因此每个类都有以下接口 Public Interface IBook Private ProductList() as collection (of Products) Private Somethingelse() as String End Interface Public Interface IProduc

我有以下三门课

Book
Product
SpecialOptions
书很多,每本书都有很多产品。同样,在一种产品中也有许多不同的品种。这三个类中的每一个都有其他属性,因此每个类都有以下接口

Public Interface IBook
   Private ProductList() as collection (of Products)
   Private Somethingelse() as String
End Interface

Public Interface IProduct
   Private SpecialOptionsList() as collection (of SpecialOptions)
   Private Somethingelse() as String
End Interface

Public Interface ISpecialOptions
   Private SpecialOptionsProperty() as String
End Interface
我想创建一个书籍集合,其中包含每个产品,当我从数据库中提取数据时,我希望在每个产品下包含相关的物种。我无法决定哪种方法是最好的

我有两种方法。要么自上而下,要么自下而上。也就是说,我从一本书开始,然后填写产品信息,然后为每个产品填写详细信息。或者我可以先获取详细信息,然后将它们添加到相应的产品中,然后再将产品添加到书籍中。这两个都不是很吸引人

而且,因为我在校对时向自己建议,这是我需要捕捉实际关系的结构,所以用不同的结构重新思考问题是行不通的。

你说过

…从一本书开始,然后填写 产品信息,然后为 这些产品中的每一种都填写了 详细信息

我会从这本书开始,然后深入研究。这将允许您仅过滤必要的信息


我希望这能回答你的问题。

有几种方法可以解决这个问题:

加载所有书籍,然后在创建每个书籍对象时加载所有产品,然后在创建每个产品对象时加载特殊选项。这将导致对数据库进行大量查询,但这很简单,并使数据库存储过程保持简单。Psudo代码:

foreach bookR in GetBooks
    Add a new book to the book collection
    foreach product in GetProductByBook
        Add a new product to the book's product collection
        foreach option in GetOptionByProduct
            Add a new option to the product option collection

在一个存储过程中加载所有书籍、书籍的所有产品和产品的所有选项,返回3个结果集。然后,首先创建您的所有书籍,然后通过在您的藏书集中找到正确的书籍并将其添加到其中来创建产品。你们的产品也一样。Psudo代码:

foreach bookR in GetResultSet1
    Add a new book to the book collection
foreach productR in GetResultSet2
    Find the correct book in book collection
    Add a new product to the book's product collection
foreach option in GetResultSet3
    Find the correct book in the book collection
    Find the correct product in the book's product collection
    Add a new option to the product option collection
results = GetResultSet
foreach result in results
    get the book for matching result book id
    if the book does not exist, create it from result
    get the product on the book for the matching result product id
    if the product does not exist, create it from result
    etc

在一个结果集中返回所有数据(LINQ到Sql就是这样做的)。将所有三个表连接在一起,并返回一个结果集。浏览每一行,检查是否存在与该行匹配的图书(如果不创建该行),然后检查该图书上是否存在产品(如果不创建该产品)。Psudo代码:

foreach bookR in GetResultSet1
    Add a new book to the book collection
foreach productR in GetResultSet2
    Find the correct book in book collection
    Add a new product to the book's product collection
foreach option in GetResultSet3
    Find the correct book in the book collection
    Find the correct product in the book's product collection
    Add a new option to the product option collection
results = GetResultSet
foreach result in results
    get the book for matching result book id
    if the book does not exist, create it from result
    get the product on the book for the matching result product id
    if the product does not exist, create it from result
    etc