Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3277186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:20:30+00:00 2026-05-17T19:20:30+00:00

How would I write a query using entity framework where I need to make

  • 0

How would I write a query using entity framework where I need to make a list with a column for the product, category and parent category. I have not figured out how to get the parent category. Any Help is appreciated.
So far I have the following:

from product in Products
select new { Ctg = (from prdCategory in ProductCategories
                    where prdCategory.Products.Contains(product)
                    select prdCategory.CategoryName).FirstOrDefault(),
             Name = product.ProductName
             ParentCtg = ...
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:20:31+00:00Added an answer on May 17, 2026 at 7:20 pm

    Ok, if all the associations has been set up correctly from your database then that’s going to be one easy query:

    var product = from p in context.Products
                  select new {
                     Name = product.ProductName,
                     CategoryNames = p.ProductCategories
                             .Select(c => c.CategoryName).ToList(),
                     ParentCategories = p.ProductCategories
                             .Select(c => c.ProductCategory2.CategoryName).ToList()
                  };
    

    When EF maps Self-Referencing Associations, it creates two relevant navigation properties named ProductCategory1 and ProductCategory2. Neither of these names is particularly helpful, one of these navigation properties refers to the parent category or the 0..1 side of the relationship. The other refers to the children or the * side of the relationship.

    To understand which is which, right-click ProductCategory1, in the property window, the multiplicity for ProductCategory1 is * (many), so ProductCategory1 represents the navigation property for the children or subcategories (also it’s of type EntityCollection<ProductCategory>) and the other one – ProductCategory2 – represents parent category for this category and it’s of type ProductCategory. For your query, we are interested in this one.

    In addition – to make your query more readable – you can rename ProductCategory1 to Subcategories and ProductCategory2 to ParentCategory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Entity Framework 4 in a project, and I have one specific query
I would like to write a hql query using a dynamic instantiation with a
I would like to write a query in a Rails model using mongoid, and
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
How would I write the following query using Linq to SQL? UPDATE [TB_EXAMPLE] SET
How would I write this query using MetaWhere (in Rails 3)? SELECT users.* FROM
I need to write a query to retrieve a big list of ids. We
I have a blog application that models a database using Entity Framework. The problem
I am using SL 4, WCF RIA Services against Entity Framework 4.0. I have
I have an entity model and I've been looking to write a linq query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.