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 8921131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:27:37+00:00 2026-06-15T06:27:37+00:00

I have a database table like this: Entity ——————— ID int PK ParentID int

  • 0

I have a database table like this:

Entity
---------------------
ID        int      PK
ParentID  int      FK
Code      varchar
Text      text

The ParentID field is a foreign key with another record in the same table (recursive). So the structure represents a Tree.

I’m trying to write a method to query this table and get 1 specific Entity based on a path. A path would be a string representing the Code properties of the Entity and the parent Entities. So an example path would be "foo/bar/baz" which means the one specific Entity of which the Code == "baz", the parent’s Code == "bar" and the parent of the parent’s Code == "foo".

My attempt:

public Entity Single(string path)
{
 string[] pathParts = path.Split('/');
 string code = pathParts[pathParts.Length -1];

 if (pathParts.Length == 1)
  return dataContext.Entities.Single(e => e.Code == code && e.ParentID == 0);

 IQueryable<Entity> entities = dataContext.Entities.Where(e => e.Code == code);
 for (int i = pathParts.Length - 2; i >= 0; i--)
 {
  string parentCode = pathParts[i];
  entities = entities.Where(e => e.Entity1.Code == parentCode); // incorrect
 }

 return entities.Single();
}

I know this isn’t correct because the Where inside the forloop just adds more conditions to the current Entity instead of the parent Entity, but how do I correct this? In words I would like the for-loop to say “and the parent’s code must be x and the parent of that parent’s code must be y, and the parent of that parent of that parent’s code must be z …. etc”. Besides that, for performance reasons I’d like it to be one IQueryable so there will be just 1 query going to the database.

  • 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-06-15T06:27:40+00:00Added an answer on June 15, 2026 at 6:27 am

    How to formulate an IQueryable to query a recursive database table?
    I’d like it to be one IQueryable so there will be just 1 query going
    to the database.

    I don’t think traversing an hierarchical table using a single translated query is currently possible with Entity Framework. The reason is you’ll need to implement either a loop or recursion and to my best knowledge neither can be translated into an EF object store query.

    UPDATE

    @Bazzz and @Steven got me thinking and I have to admit I was completely wrong: it is possible and quite easy to construct an IQueryable for these requirements dynamically.

    The following function can be called recursively to build up the query:

    public static IQueryable<TestTree> Traverse(this IQueryable<TestTree> source, IQueryable<TestTree> table, LinkedList<string> parts)
    {
        var code = parts.First.Value;
        var query = source.SelectMany(r1 => table.Where(r2 => r2.Code == code && r2.ParentID == r1.ID), (r1, r2) => r2);
        if (parts.Count == 1)
        {
            return query;
        }
        parts.RemoveFirst();
        return query.Traverse(table, parts);
    }
    

    The root query is a special case; here’s a working example of calling Traverse:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var parts = new LinkedList<string>(path.Split('/'));
        var table = context.TestTrees;
    
        var code = parts.First.Value;
        var root = table.Where(r1 => r1.Code == code && !r1.ParentID.HasValue);
        parts.RemoveFirst();
    
        foreach (var q in root.Traverse(table, parts))
            Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    The DB is queried only once with this generated code:

    exec sp_executesql N'SELECT 
    [Extent3].[ID] AS [ID], 
    [Extent3].[ParentID] AS [ParentID], 
    [Extent3].[Code] AS [Code]
    FROM   [dbo].[TestTree] AS [Extent1]
    INNER JOIN [dbo].[TestTree] AS [Extent2] ON ([Extent2].[Code] = @p__linq__1) AND ([Extent2].[ParentID] = [Extent1].[ID])
    INNER JOIN [dbo].[TestTree] AS [Extent3] ON ([Extent3].[Code] = @p__linq__2) AND ([Extent3].[ParentID] = [Extent2].[ID])
    WHERE ([Extent1].[Code] = @p__linq__0) AND ([Extent1].[ParentID] IS NULL)',N'@p__linq__1 nvarchar(4000),@p__linq__2 nvarchar(4000),@p__linq__0 nvarchar(4000)',@p__linq__1=N'bar',@p__linq__2=N'baz',@p__linq__0=N'foo'
    

    And while I like the execution plan of the raw query (see below) a bit better, the approach is valid and perhaps useful.

    End of UPDATE

    Using IEnumerable

    The idea is to grab the relevant data from the table in one go and then do the traversing in the application using LINQ to Objects.

    Here’s a recursive function that will get a node from a sequence:

    static TestTree GetNode(this IEnumerable<TestTree> table, string[] parts, int index, int? parentID)
    {
        var q = table
            .Where(r => 
                 r.Code == parts[index] && 
                 (r.ParentID.HasValue ? r.ParentID == parentID : parentID == null))
            .Single();
        return index < parts.Length - 1 ? table.GetNode(parts, index + 1, q.ID) : q;
    }
    

    You can use like this:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var q = context.TestTrees.GetNode(path.Split('/'), 0, null);
        Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    This will execute one DB query for each path part, so if you want the DB to only be queried once, use this instead:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var q = context.TestTrees
            .ToList()
            .GetNode(path.Split('/'), 0, null);
        Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    An obvious optimization is to exclude the codes not present in our path before traversing:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var parts = path.Split('/');
        var q = context
            .TestTrees
            .Where(r => parts.Any(p => p == r.Code))
            .ToList()
            .GetNode(parts, 0, null);
        Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    This query should be fast enough unless most of your entities have similar codes. However, if you absolutely need top performance, you could use raw queries.

    SQL Server Raw Query

    For SQL Server a CTE-based query would probably be best:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var q = context.Database.SqlQuery<TestTree>(@"
            WITH Tree(ID, ParentID, Code, TreePath) AS
            (
                SELECT ID, ParentID, Code, CAST(Code AS nvarchar(512)) AS TreePath
                FROM dbo.TestTree
                WHERE ParentID IS NULL
    
                UNION ALL
    
                SELECT TestTree.ID, TestTree.ParentID, TestTree.Code, CAST(TreePath + '/' + TestTree.Code AS nvarchar(512))
                FROM dbo.TestTree
                INNER JOIN Tree ON Tree.ID = TestTree.ParentID
            )
            SELECT * FROM Tree WHERE TreePath = @path", new SqlParameter("path", path)).Single();
        Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    Limiting data by the root node is easy and might be quite useful performance-wise:

    using (var context = new TestDBEntities())
    {
        var path = "foo/bar/baz";
        var q = context.Database.SqlQuery<TestTree>(@"
            WITH Tree(ID, ParentID, Code, TreePath) AS
            (
                SELECT ID, ParentID, Code, CAST(Code AS nvarchar(512)) AS TreePath
                FROM dbo.TestTree
                WHERE ParentID IS NULL AND Code = @parentCode
    
                UNION ALL
    
                SELECT TestTree.ID, TestTree.ParentID, TestTree.Code, CAST(TreePath + '/' + TestTree.Code AS nvarchar(512))
                FROM dbo.TestTree
                INNER JOIN Tree ON Tree.ID = TestTree.ParentID
            )
            SELECT * FROM Tree WHERE TreePath = @path", 
                new SqlParameter("path", path),
                new SqlParameter("parentCode", path.Split('/')[0]))
                .Single();
        Console.WriteLine("{0} {1} {2}", q.ID, q.ParentID, q.Code);
    }
    

    Footnotes

    All of this was tested with .NET 4.5, EF 5, SQL Server 2012. Data setup script:

    CREATE TABLE dbo.TestTree
    (
        ID int not null IDENTITY PRIMARY KEY,
        ParentID int null REFERENCES dbo.TestTree (ID),
        Code nvarchar(100)
    )
    GO
    
    INSERT dbo.TestTree (ParentID, Code) VALUES (null, 'foo')
    INSERT dbo.TestTree (ParentID, Code) VALUES (1, 'bar')
    INSERT dbo.TestTree (ParentID, Code) VALUES (2, 'baz')
    INSERT dbo.TestTree (ParentID, Code) VALUES (null, 'bla')
    INSERT dbo.TestTree (ParentID, Code) VALUES (1, 'blu')
    INSERT dbo.TestTree (ParentID, Code) VALUES (2, 'blo')
    INSERT dbo.TestTree (ParentID, Code) VALUES (null, 'baz')
    INSERT dbo.TestTree (ParentID, Code) VALUES (1, 'foo')
    INSERT dbo.TestTree (ParentID, Code) VALUES (2, 'bar')
    

    All examples in my test returned the ‘baz’ entity with ID 3. It’s assumed that the entity actually exists. Error handling is out of scope of this post.

    UPDATE

    To address @Bazzz’s comment, the data with paths is shown below. Code is unique by level, not globally.

    ID   ParentID    Code      TreePath
    ---- ----------- --------- -------------------
    1    NULL        foo       foo
    4    NULL        bla       bla
    7    NULL        baz       baz
    2    1           bar       foo/bar
    5    1           blu       foo/blu
    8    1           foo       foo/foo
    3    2           baz       foo/bar/baz
    6    2           blo       foo/bar/blo
    9    2           bar       foo/bar/bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
i have database table like this +-------+--------------+----------+ | id | ip | date |
I always forget how to do things like this. I have a database table
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a question. My database table is something like this: | ID |
I have a table like this in my database (SQL Server 2008) ID Type
I have a database application in which a group is modeled like this: TABLE
I have written a function to print database table to an array like this
I have a table in my database like this: [id] [uniqueidentifier] NOT NULL, [user_id]
I have a class and subclasses who extends that. Like this: @Table @Entity class

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.