Recursive Linq query

4 posts, 0 answers
  1. Ujjwal Manna
    Ujjwal Manna avatar
    8 posts
    Member since:
    Dec 2009

    Posted 02 Feb 2010 Link to this post

    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

     

  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 02 Feb 2010 Link to this post

    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.
  3. DevCraft banner
  4. Ujjwal Manna
    Ujjwal Manna avatar
    8 posts
    Member since:
    Dec 2009

    Posted 05 Feb 2010 Link to this post

    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

  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 05 Feb 2010 Link to this post

    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.
Back to Top