在连接单个表时使用LINQ的不同方式

时间:2014-05-12 05:20:29

标签: vb.net linq

给出ProductCategories的表结构......

    ProductCategoryID
    ParentProductCategoryID
    Name
    ModifiedDate

除了以下内容之外,还有其他方法可以编写查询以列出所有父类别及其子类别......

    From categories In context.ProductCategories
    Let subcategories = categories.ProductCategories
    Where subcategories.Any()
    Select categories.Name, subcategories = (From sub In subcategories 
                                             Select sub.Name)

1 个答案:

答案 0 :(得分:1)

尝试:

From category in context.ProductCategories
    Group category By key1 = category.ParentProductCategoryID, key2 = category.ProductCategoryID Into Group
    Select New With {.ParentProductCategoryID= key1, .ProductCategoryID = key2}