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

  • SEARCH
  • Home
  • 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 7010225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:57:50+00:00 2026-05-27T21:57:50+00:00

I work in C# and Entity framework. I have a table in my database

  • 0

I work in C# and Entity framework.
I have a table in my database named Genre. Here are its attributes:
idGenre, name, idParentGenre.

For example. the values would be:

(idGenre = 1, name = “acoustic”, idParentGenre=2)

(idGenre = 2, name = “rock”, idParentGenre=2)

(idGenre = 3, name = “country”, idParentGenre=4)

(idGenre = 4, name = “folk”, idParentGenre=5)

(idGenre = 5, name = “someOtherGenre”, idParentGenre=5)

As you can see, it’s kind of a tree.

Now, I have a method for searching through this table. The input parameter is idGenre and idParentGenre. I should return if the genre (idGenre) is a son/grandchild/grandgrandchild/… of idParentGenre.

For example, I get idGenre=3, idParentGenre=5, I should return true.

However, there isn’t recursion in Linq. Is there a way I can do this?

  • 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-27T21:57:51+00:00Added an answer on May 27, 2026 at 9:57 pm

    I would make a method to handle this instead of using LINQ:

    bool HasParent(int genre, int parent)
    {
        Genre item = db.Genres.FirstOrDefault(g => g.IdGenre == genre);
        if (item == null)
            return false;
    
        // If there is no parent, return false, 
        // this is assuming it's defined as int?
        if (!item.idParentGenre.HasValue)
            return false;
    
        if (item.idParentGenre.Value == parent)
            return true;
    
        return HasParent(item.idParentGenre, parent);
    }
    

    This lets you handle this in a single recursive function.

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

Sidebar

Related Questions

Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
I work with Entity Framework - add records in the database using classes that
I have the following in Entity Framework . Table - Country Fields List item
Background: Entity Framework 4, with SQL Server 2008 Problem: I have a table Order
I'm starting to work with Entity Framework as ORM. But my question is: is
I've been driving myself crazy trying to get the Entity Framework to work as
We been having some discussions on approaches to using the entity framework at work
I know the method below isn't meant to work with any Entity, and its
I am using Entity Framwork DbContexts with a legacy database. I have 2 different
I have created a Entity Framework 4 model with Visual Studio 2010 and generated

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.