none
Hierarchical query in Linq to get child elements RRS feed

  • Question

  • Hi,

    I have a BindingList of employees. I want to update the employees of a chosen department and chosen location under a manager.

     

    So my binding List is,

    BindingList<employees>

    private int managerID,int locID,int deptID.

     

    my employee class has a bool flag  forUpdate.

    How do I select all employees under this manager and all employees under the employees of this manager to all the way down and make their flag set as forUpdate=true.

    Thanks for the help.

    Abhi

     

     

     

    Monday, January 24, 2011 8:48 AM

Answers

  • Hi Abhi,

     

    Thanks for your feedback.

    I think we could try to use the dynamic queries to get the child elements. In other word, we could use the expression tree.

    Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

    You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic).

    If you want know more information, see this: http://msdn.microsoft.com/en-us/library/bb397951.aspx

     

    Thanks for your support!

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Abhi Arjun Monday, February 7, 2011 6:13 AM
    Thursday, February 3, 2011 2:58 AM
    Moderator

All replies

  • Hello Abhi,

     

    Thank you for posting in the Linq to Sql Forum.

    Based on your description, I'm afraid I don't understand your question. I know there's parent element and child elements. Like you said, I think you are talking about using XML. Am I right?

    If I'm right, here is an example:

    <PrinterDirectory> 
     
    <Country Name="UK> 
       
    <Region Name="Aberdeen" /> 
       
    <Region Name="Birmingham" /> 
       
    <Region Name="London" /> 
     
    </Country> 
     
    <Country Name="France"> 
       
    <Region Name="Paris" /> 
       
    <Region Name="Bordeaux" /> 
     
    </Country> 
    </PrinterDirectory> 

    The simplest way is probably to use a subsequent from clause, like this:

    var regionQuery = from items in xdoc.Descendants("Country") 
                      where items
    .Attribute("Name").Value == "UK" 
                     
    from region in items.Elements("Region") 
                     
    select region.Attribute("Name").Value; 

    var regionQuery = from items in xdoc.Descendants("Manager") 
                      where items
    .Attribute("Name").Value == "Jack" 
                     
    from region in items.Elements("Region") 
                     
    select region.Attribute("Name").Value; 
     
    Please feel free to tell me if I understood you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 26, 2011 2:48 PM
    Moderator
  • Hi Jackie,

     

    I will rephrase my question.

     

    Public Class Employee

    {

         public int EmpID{get;set;}

         public string EmpName{get;set;}

         public int MgrID {get;set;}

         public bool forUpdate {get;set;}

    }

     

    Partial class MainForm:Window

    {

           public BindingList<Employee> employees=new public BindingList<Employee>();

           MainForm()

          {

               LoadEmployeeBindingList();

           }

           Private string updateBindingList(string manager)

          {

          }

     

    }

     

    So in the mainform the bindinglist will be filled on load. I need the code in UpdateBindingList where the manager name will be supplied. Say if the manger name is "Jack" my intention is to get the employees where he is the manager, manger's manger ...etc.  Also I need to update the BindingList forUpdate=true if a match is found. This is not a XML

    Wednesday, February 2, 2011 11:25 AM
  • Hi Abhi,

     

    Thanks for your feedback.

    I think we could try to use the dynamic queries to get the child elements. In other word, we could use the expression tree.

    Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

    You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to: Use Expression Trees to Build Dynamic Queries (C# and Visual Basic).

    If you want know more information, see this: http://msdn.microsoft.com/en-us/library/bb397951.aspx

     

    Thanks for your support!

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Abhi Arjun Monday, February 7, 2011 6:13 AM
    Thursday, February 3, 2011 2:58 AM
    Moderator