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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:01:11+00:00 2026-05-20T08:01:11+00:00

I have some hierarchical data that I need to return via a stored procedure

  • 0

I have some hierarchical data that I need to return via a stored procedure (because straight LINQ-to-SQL can’t really handle the recursion). Lets say these are the two objects SQLMetal generates that I’m working with:

class TableA_Record
{
    public IEnumerable<Child_TableB> Child_TableB_Records;
}

class TableB_Record
{
    public TableA_Record parent;
}

Is it possible to create a stored procedure that will return something like an IEnumerable, but also pulls the Child_TableB_Records along with it and populates those as well? Kind of similar to what would happen in this code, but using a stored procedure:

using(DataContext db = new DataContext())
{
    DataLoadOptions dlOptions = new DataLoadOptions();

    dlOptions.LoadWith<TableA_Record>(x => x.Child_TableB_Records);

    db.loadOptions = dlOptions;

    var query = from a in db.TableA
                select a;
}

Any examples or links to tutorials would be highly appreciated if this is possible. Thanks in advance!

  • 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-20T08:01:12+00:00Added an answer on May 20, 2026 at 8:01 am

    I don’t believe what you are trying to achieve is possible with Linq2Sql.

    However a possible work around would be to use the IMultiResult interface. So you would set up your stored procedure to return two sets of results (TableA_Record and TableB_Records). You add the stored procedure to the DBML and alter the call to the stored procedure to be cast as IMultiResult and depending on the attributes you decorate the method with the Linq results will be populated in those objects.

    There is an example of this by Scott Gu LINQ to SQL (Part 6 – Retrieving Data Using Stored Procedures) (Section named “Handling Multiple Result Shapes from stored procedures”)

    You would then have to take an additional step and map the objects into the structure that you are looking for.

    There are additional steps that you need to take but I believe this is closest to what you are trying to achieve as you return one set of results that you are then working with.

    Edit:

    One thing to note is the ordering of the ResultType attribute is important. The order result sets are returned is the order that the ResultType attribute should be used to decorate the method. If you get the ordering incorrect the objects will just return as null and no exception will be thrown (I have had issues with this happening before).

    Here are some additional examples:

    • Linq to SQL Stored Procedures with Multiple Results – IMultipleResults
    • IMultiResult return Stored Procedure Results
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project where I have some hierarchical data that I want
I have some hierarchical data - each entry has an id and a (nullable)
We have some input data that sometimes appears with &nbsp characters on the end.
We have some files on our website that users of our software can download.
I have some UI in VB 2005 that looks great in XP Style, but
I have some kind of test data and want to create a unit test
I have a multi-thread C# application that uses some recursive functions in a dll.
I have a table containing hierarchical data. There are currently ~8 levels in this
I have some ASP.NET web services which all share a common helper class they
I have some code for starting a thread on the .NET CF 2.0: ThreadStart

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.