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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:14:52+00:00 2026-05-10T17:14:52+00:00

I have some hierarchical data – each entry has an id and a (nullable)

  • 0

I have some hierarchical data – each entry has an id and a (nullable) parent entry id. I want to retrieve all entries in the tree under a given entry. This is in a SQL Server 2005 database. I am querying it with LINQ to SQL in C# 3.5.

LINQ to SQL does not support Common Table Expressions directly. My choices are to assemble the data in code with several LINQ queries, or to make a view on the database that surfaces a CTE.

Which option (or another option) do you think will perform better when data volumes get large? Is SQL Server 2008’s HierarchyId type supported in Linq to SQL?

  • 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. 2026-05-10T17:14:53+00:00Added an answer on May 10, 2026 at 5:14 pm

    I would set up a view and an associated table-based function based on the CTE. My reasoning for this is that, while you could implement the logic on the application side, this would involve sending the intermediate data over the wire for computation in the application. Using the DBML designer, the view translates into a Table entity. You can then associate the function with the Table entity and invoke the method created on the DataContext to derive objects of the type defined by the view. Using the table-based function allows the query engine to take your parameters into account while constructing the result set rather than applying a condition on the result set defined by the view after the fact.

    CREATE TABLE [dbo].[hierarchical_table](     [id] [int] IDENTITY(1,1) NOT NULL,     [parent_id] [int] NULL,     [data] [varchar](255) NOT NULL,  CONSTRAINT [PK_hierarchical_table] PRIMARY KEY CLUSTERED  (     [id] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY]  CREATE VIEW [dbo].[vw_recursive_view] AS WITH hierarchy_cte(id, parent_id, data, lvl) AS (SELECT     id, parent_id, data, 0 AS lvl       FROM         dbo.hierarchical_table       WHERE     (parent_id IS NULL)       UNION ALL       SELECT     t1.id, t1.parent_id, t1.data, h.lvl + 1 AS lvl       FROM         dbo.hierarchical_table AS t1 INNER JOIN                             hierarchy_cte AS h ON t1.parent_id = h.id) SELECT     id, parent_id, data, lvl FROM         hierarchy_cte AS result   CREATE FUNCTION [dbo].[fn_tree_for_parent]  (     @parent int ) RETURNS  @result TABLE  (     id int not null,     parent_id int,     data varchar(255) not null,     lvl int not null ) AS BEGIN     WITH hierarchy_cte(id, parent_id, data, lvl) AS    (SELECT     id, parent_id, data, 0 AS lvl         FROM         dbo.hierarchical_table         WHERE     (id = @parent OR (parent_id IS NULL AND @parent IS NULL))         UNION ALL         SELECT     t1.id, t1.parent_id, t1.data, h.lvl + 1 AS lvl         FROM         dbo.hierarchical_table AS t1 INNER JOIN             hierarchy_cte AS h ON t1.parent_id = h.id)     INSERT INTO @result     SELECT     id, parent_id, data, lvl     FROM         hierarchy_cte AS result RETURN  END  ALTER TABLE [dbo].[hierarchical_table]  WITH CHECK ADD  CONSTRAINT [FK_hierarchical_table_hierarchical_table] FOREIGN KEY([parent_id]) REFERENCES [dbo].[hierarchical_table] ([id])  ALTER TABLE [dbo].[hierarchical_table] CHECK CONSTRAINT [FK_hierarchical_table_hierarchical_table] 

    To use it you would do something like — assuming some reasonable naming scheme:

    using (DataContext dc = new HierarchicalDataContext()) {     HierarchicalTableEntity h = (from e in dc.HierarchicalTableEntities                                  select e).First();     var query = dc.FnTreeForParent( h.ID );     foreach (HierarchicalTableViewEntity entity in query) {         ...process the tree node...     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The following code in a servlet filter has worked for… May 11, 2026 at 2:39 pm
  • added an answer Use the PHP filter functions. You can use them for… May 11, 2026 at 2:39 pm
  • added an answer I used the advice from this article to get an… May 11, 2026 at 2:39 pm

Related Questions

I have some hierarchical data - each entry has an id and a (nullable)
I'm working on a project that has to connect to some ancient webservices that
I am trying to come up with the best way to render some hierarchical
I’m creating a UserControl for a rich TreeView (one that has context menus for
I have a task at hand of creating some kind of logic for a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.