Question

Given the table structure of ProductCategories...

    ProductCategoryID
    ParentProductCategoryID
    Name
    ModifiedDate

Is there any other way to write a query for listing all parent categories and their subcategories besides the following...

    From categories In context.ProductCategories
    Let subcategories = categories.ProductCategories
    Where subcategories.Any()
    Select categories.Name, subcategories = (From sub In subcategories 
                                             Select sub.Name)
Était-ce utile?

La solution

Try:

From category in context.ProductCategories
    Group category By key1 = category.ParentProductCategoryID, key2 = category.ProductCategoryID Into Group
    Select New With {.ParentProductCategoryID= key1, .ProductCategoryID = key2}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top