Linq recursive query

Hi Experts,

My Sql table looks like this:
CategoryId
ParentCategoryId
CategoryName

I have a categoryId and I want to list all the parents up the root category.

I use linq and I know how to find the first parent category, but I want to find all the parents to get the complete path.

Thanks in advance for your help.
LVL 9
nouloukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nouloukAuthor Commented:
Thanks Smart Man,

I already found those links, but it doesn't help me. It seems to be very difficult and not talk about linq to Sql.
var ParentCategory = from categories in dataContext.Categories
                                   where categories.CategoryId = categoryId
                                   select categories.ParentCategoryId

The recursion means to reuse this query to get the parent of this ParentCategoryId from my submitted CategoryId ...

Your link doesn't show any linq to Sql queries or could you tell me the way to implement such a recursive query?


0
Smart_ManCommented:
it is about traversing operator

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1686337&S

http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx


There has not been a recursive query operator defined yet for LINQ.  Of course, you've defined a cyclic data structure, so even if there were a recursive operator it would likely fail.

 

Here is a recursive traversal operator you could use.  It may have some typos.  It converts your deep hierarchy into a flat sequence.



public static class MyExtensions {

   public static IEnumerable<T> Traverse(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse) {

           foreach(T item in source) {

                  yield return item;

                  IEnumerable<T> seqRecurse = fnRecurse(item);

                  if (seqRecurse != null) {

                      foreach(T itemRecurse in Traverse(seqRecurse, fnRecurse)) {

                         yield return itemRecurse;

                      }

                  }

           }

}

 

To traverse all values in your collection:

 

List<MyClass> list = ...;

 

var total = list.Traverse(x => x.MyList).Sum(x => x.Val).


hope that is more helpfull. sorry the previous links did not worked for you.

let me know if you want the solution code supplied directly

waiting for your reply
0
nouloukAuthor Commented:
Thanks, your comments help me very much.

The main problem is I use Linq to SQL and using an extension as you tell is crazy.
For example, if I have 20 childs, Linq to SQL will query 20 requests againt the database !
So, as you suggest me I will use a stored procs with CTE.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.