title: How to Implement self join to obtain complete result using Linq c#?
body: <p>In my scenario I need to show all the employees whose manager id is @Id.</p>
  
  <p>The hierarchy is like</p>
  
  <pre><code>Manager --- Employee
  john --- smith
  john --- sam
  sam --- peru
  sam --- karim
  </code></pre>
  
  <p>I am using linq and Entity Framework DB first. </p>
  
  <p>I need to show all the employees under particular manager let take it john with <code>id = 2</code></p>
  
  <p>I apply 2 different queries but result is same ie it is showing only 1st hierarchy john is getting only 2 employees smith and sam but the employees inside sam must also be displayed are not retrieving.. </p>
  
  <p>Here is My code:</p>
  
  <pre><code>var lstAllUser = (List&lt;Entities.User&gt;)Session["AllUsers"];
  
  int pID = Convert.ToInt32(ddlSelectedValue);
  
   //query 1 which i try first        
  var lstSelectedEmployees1 = lstAllUser.Where(emp =&gt; emp.ManagerId == pID)
                                        .Select(emp =&gt; new { 
                                            EmployeeName = emp.UserDetail.Name, 
                                            ManagerName = emp.Manager.UserDetail.Name 
                                      }).ToList();
  
  //query 2 
  var lstSelectedEmployees = (from employee in lstAllUser
                              where employee.ManagerId == pID
                              join e1 in lstAllUser 
                              on employee.ManagerId equals e1.UserID
                              select new
                              {
                                  ManagerName = e1.UserDetail.Name,
                                  EmployeeName = employee.UserDetail.Name
                              }).ToList();
  
  
  gvEmployeeManager.DataSource = lstSelectedEmployees;
  gvEmployeeManager.DataBind();
  </code></pre>
  
  <p>Aspx is like;</p>
  
  <pre><code>&lt;asp:GridView ID="gvEmployeeManager" runat="server" AutoGenerateColumns="False" GridLines="Vertical" CssClass="table table-striped"&gt;
  &lt;Columns&gt;
      &lt;asp:BoundField DataField="EmployeeName" HeaderText="Employee" /&gt;
      &lt;asp:BoundField DataField="ManagerName" HeaderText="Manager" /&gt;
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Help required!!</p>