This is a migrated thread and some comments may be shown as answers.

Recursive Linq query

3 Answers 141 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ujjwal Manna
Top achievements
Rank 1
Ujjwal Manna asked on 02 Feb 2010, 09:37 AM
Hi All,

Presently I am stuck in a linq query. It would be great if you can help on this.

My Sql table looks like this:
NameId
ParentNameId
Name

Now I have to write a query where user will give the Name as input and I have to return all the parents up to the root NameId.
[ex :
     Ujjwal
        Ram
           Andy
             Bob
         Sam
             Mathew

Now if user give the input as "Andy". I have to return the result from linq query which contain below information :

  Ujjwal
     Ram
       Andy

Thanks in advance for your help.

Regards,
Ujjwal

 

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 02 Feb 2010, 07:22 PM
Hello Ujjwal Manna,

Unfortunately the Linq framework does not support recursive queries yet and you will have to use a recursive method which contains a Linq query. This method would look similar to this:

public void Walker(IObjectScope scope, string names)
{
 var nameList = scope.Extent<Name>().Where(n => n.Names == names);
 foreach (var name in nameList)
 {
  if (name != null)
  {
   Console.WriteLine(name.Names);
   if (name.ParentNameId != null)
   {
    Walker(scope, name.Name1.Names);
   }
  }   
 }
}
Hope that helps.

Best wishes,
Damyan Bogoev
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Ujjwal Manna
Top achievements
Rank 1
answered on 05 Feb 2010, 11:08 AM
Hi All,
Thanks a lot for your kind help. I need one more help from you. Now I want to write one more linq query where it will return all the records including its children.

For example :

Ujjwal
        Ram
           Andy
                Bob
                  Jim
            Das
               Merry
         Sam
             Mathew

For the above structure  I have to write a function where I will pass the id of the Parent node and it will return all childrens which is belongs to that parent. Now if I pass the id of "Ram" it will return the results like below :

    Ram
           Andy
                Bob
                  Jim
            Das
               Merry

I have already tried with different approch but nothing works properly,so it would be greate if you can help on this or if you can send me any link.

Regards,
Ujjwal

0
Damyan Bogoev
Telerik team
answered on 05 Feb 2010, 09:11 PM
Hi Ujjwal Manna,

You could use the following method in order to achieve the inquired traversal:

public void TraverseChilds(IObjectScope scope, string names)
{
    Console.WriteLine(names);
    var childList = scope.Extent<Name>().Where(n => n.ParentReference.Names == names);
    foreach (var name in childList)
    {
        if (name != null)
        {
            TraverseChilds(scope, name.Names);
        }
    }
}

I think that will help you.

Greetings,
Damyan Bogoev
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
LINQ (LINQ specific questions)
Asked by
Ujjwal Manna
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Ujjwal Manna
Top achievements
Rank 1
Share this question
or