Microsoft Developer Network > Forums Home > Archived Forums Forums > LINQ Project General > Using LINQ to query MULTI LEVEL object hierarchy
Ask a questionAsk a question
 

AnswerUsing LINQ to query MULTI LEVEL object hierarchy

  • Thursday, October 29, 2009 6:27 AMVipul Mehta Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I have an MULTI LEVEL hierarchical collection object - List<Application> apps = new List<Application>(); The Application object contains following properties - int ApplicationId, List<Application> Applications, string AppName and so on..
     
    When my application starts, the apps "multi level"  collection object gets filled up. Now on a particular action on an page, I need to get/search for an Application object that has ApplicationId = 308 (in real time this number would come dynamically). This is easily possible by looping through the collection object & comparing the ApplicationId property with variable holding value 308. But I wanted to know if this is possible using LINQ query to get an Application object with ApplicationId as 308?
    I tried searching on this issue but came across examples with fixed hierarchy level.
     
    Your suggestion/pointers to this issue would be highly appreciated
     
    Note:
    1. The level of the object hierarchy is not fixed and can vary.
    2. ApplicationId would be UNIQUE for all Application object(s) in the hierarchy.

     
    Regards,
    Vipul Mehta
    • Moved byNoam Ben-Ami - MSFTMSFTWednesday, November 04, 2009 6:01 PM (From:ADO.NET Entity Framework and LINQ to Entities)
    •  

Answers

  • Friday, October 30, 2009 5:45 AMYichun_FengMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Vipul,

     

    For your issue, it is more convenient to use recursion for searching.

    If you want to build dynamic Linq for it, it is not easy to achieve, because common dynamict Linq library is generally used for IQueryable<T> queries like LINQ to SQL and LINQ to Entities.

    The recursion example is as follows,

     

                    private void Form1_Load(object sender, EventArgs e)

            {

                List<MyApplication> L0 = new List<MyApplication>();

     

                List<MyApplication> L1 = new List<MyApplication>();

                L1.Add(new MyApplication(){id = 11, name ="L11"});

     

                List<MyApplication> L2 = new List<MyApplication>();

                L2.Add(new MyApplication(){id = 21, name = "L21"});

     

                L1.Add(new MyApplication(){id = 12, name="L21", appList= L2});

     

                L0.Add(new MyApplication(){id = 1, name = "L01", appList = L1});

     

                MyApplication res = null;

                foreach(MyApplication a in L0)

                {

                    res = SerarchApplication(a, 12);

                    if (res != null)

                        break;

                }

                Console.WriteLine("{0}", res.name );

     

                Console.WriteLine();

     

            }

     

     

            public MyApplication SerarchApplication(MyApplication app, int resid)

            {

                MyApplication res = null;

     

                if (app.id == resid)

                {

                    return app;

                }

                else if (app.appList != null)

                {

                    foreach (MyApplication a in app.appList)

                    {

                        res = SerarchApplication(a, resid);

                       if (res != null)

                           return res;

                    }

                }

                return null;

            }

     

    Also, it is recommended to use MyApplication as the class name, because the Application is the reserved word in C# programming.

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Thursday, October 29, 2009 8:48 AMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Perhaps local queries can help you?

    http://blogs.msdn.com/dsimmons/archive/2009/02/21/local-queries.aspx


    Please mark posts as answers/helpful if it answers your question
  • Thursday, October 29, 2009 9:38 AMYichun_FengMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Vipul,

     

    Welcome to MSDN forums!

     

    Based on your description, I’m still not sure about server points. Would you mind providing more information about the following questions?

    1)    Are you using Entity Framework or LINQ to object?

    If you just want to use LINQ to query the multi-level List, it is belonged to LINQ to object actually.

    2)    Do you mean you want to get the certain Application instance in unknown level?

     

     

    Please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, October 29, 2009 9:52 AMVipul Mehta Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Application that I mentioned above is my custom class. I am looking for LINQ to Objects.
  • Friday, October 30, 2009 5:45 AMYichun_FengMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Vipul,

     

    For your issue, it is more convenient to use recursion for searching.

    If you want to build dynamic Linq for it, it is not easy to achieve, because common dynamict Linq library is generally used for IQueryable<T> queries like LINQ to SQL and LINQ to Entities.

    The recursion example is as follows,

     

                    private void Form1_Load(object sender, EventArgs e)

            {

                List<MyApplication> L0 = new List<MyApplication>();

     

                List<MyApplication> L1 = new List<MyApplication>();

                L1.Add(new MyApplication(){id = 11, name ="L11"});

     

                List<MyApplication> L2 = new List<MyApplication>();

                L2.Add(new MyApplication(){id = 21, name = "L21"});

     

                L1.Add(new MyApplication(){id = 12, name="L21", appList= L2});

     

                L0.Add(new MyApplication(){id = 1, name = "L01", appList = L1});

     

                MyApplication res = null;

                foreach(MyApplication a in L0)

                {

                    res = SerarchApplication(a, 12);

                    if (res != null)

                        break;

                }

                Console.WriteLine("{0}", res.name );

     

                Console.WriteLine();

     

            }

     

     

            public MyApplication SerarchApplication(MyApplication app, int resid)

            {

                MyApplication res = null;

     

                if (app.id == resid)

                {

                    return app;

                }

                else if (app.appList != null)

                {

                    foreach (MyApplication a in app.appList)

                    {

                        res = SerarchApplication(a, resid);

                       if (res != null)

                           return res;

                    }

                }

                return null;

            }

     

    Also, it is recommended to use MyApplication as the class name, because the Application is the reserved word in C# programming.

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, November 06, 2009 3:31 AMYichun_FengMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Vipul,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

    Best Regards

    Yichun Feng

    MSDN Subscriber Support in Forum 

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.