none
Many to Many tables relationship LINQ RRS feed

  • Question

  • Let's say that I have three tables:

    • Categories
    • SubCategories
    • Categories_SubCategories (including many to many relationships within the previous tables)

    Like the following: 

    How can I get using LINQ all of the SubCategories for a specific CategoryId?

    Monday, October 12, 2020 7:42 AM

All replies

  • Hi Claudio,
    If you are using EF, you can use inner join to connect three tables.
    Please refer to the following Linq:

    var SubCategories = (from cs in _context.Categories_SubCategories 
                      inner join a in _context.Categories on a.CategoryId =cs.CategoryId
                      inner join c in _context.SubCategories on c.SubCategoryId =cs.CategoryId 
                      where cs.CategoryId==1
                      select c).ToList();

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 13, 2020 2:08 AM
  • In the event you are working with Entity Framework Core you can traverse the graph using .Include and .ThenInclude. For instance using Orders table with related tables e.g. Employees, Shippers and order details looing by order id.

    using (var context = new NorthwindContext())
    {
        Orders order = context
            .Orders
            .Include(ord => ord.OrderDetails)
            .Include(ord => ord.ShipViaNavigation)
            .ThenInclude(od => od)
            .Include(ord => ord.Employee)
            .FirstOrDefault(ord => ord.OrderID == 10248);
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 15, 2020 1:22 PM
  • In the event you are working with Entity Framework Core you can traverse the graph using .Include and .ThenInclude. For instance using Orders table with related tables e.g. Employees, Shippers and order details looing by order id.

    using (var context = new NorthwindContext())
    {
        Orders order = context
            .Orders
            .Include(ord => ord.OrderDetails)
            .Include(ord => ord.ShipViaNavigation)
            .ThenInclude(od => od)
            .Include(ord => ord.Employee)
            .FirstOrDefault(ord => ord.OrderID == 10248);
    
        Console.WriteLine();
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks! both responses worked.
    Saturday, October 17, 2020 1:25 AM
  • Hi Claudio,
    I am glad you have solved your problem. And we hope you can mark it as an answer. By marking a post as answered, you help others find the answer faster.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, October 19, 2020 1:05 AM