3

I'm interested in writing an extension method the IQueryable interface. The method would return all children recursively of the specified selector.

public static class MyExtensions
{
    public static IQueryable<IRecursion<T>> SelectRecursive<T>(this IQueryable<T> source, Func<T, IQueryable<T>> selector)
    {
        //Code goes here
    }

    public interface IRecursion<T>
    {
        int Depth { get; }

        T Item { get; }
    }
}

Example usage:

var allChildren = tblCompanies
        .Where(c => c.pkCompanyID == 38)
        .SelectRecursive(p => tblCompanies.Where (c => c.pkCompanyID == p.fkCompToCompID));

SQL code generated by function would be something like this.

WITH CompanyCTE(ID, parentID, depth) AS
(
    SELECT
        pkCompanyID, 
        fkCompToCompID,
        0
    FROM 
        tblCompany

    UNION ALL

    SELECT
        tblCompany.pkCompanyID, 
        tblCompany.fkCompToCompID,
        CompanyCTE.depth + 1
    FROM 
        tblCompany
        JOIN CompanyCTE ON tblCompany.fkCompToCompID = CompanyCTE.ID
)
SELECT
    tblCompany.*, --Item
    CompanyCTE.depth --Depth
FROM 
    CompanyCTE
    JOIN tblCompany ON CompanyCTE.ID = tblCompany.pkCompanyID
WHERE
    parentID = 38

Can it be done? If not possible with a CTE, maybe with SQL 2008 hierarchyid?

2 Answers 2

1

This is not possible in L2S. You can however expand the query to a certain constant depth if that is enough for you. This will result in a nasty forest of joins.

As your set of "companies" is probably not very large, try loading all of them and do this client-side.

0

See these:

http://jasonrowe.com/2009/09/19/recursive-linq-query-example/

1
  • I know how to write this using Linq to Objects (the page you are referencing). The question is about writing it for Linq to SQL (IQueryable)
    – Magnus
    Feb 4, 2011 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.