سؤال

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)
هل كانت مفيدة؟

المحلول

Try:

From category in context.ProductCategories
    Group category By key1 = category.ParentProductCategoryID, key2 = category.ProductCategoryID Into Group
    Select New With {.ParentProductCategoryID= key1, .ProductCategoryID = key2}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top