title: How to find recursive Parent Child hierarchy Dynamically using Linq in Mvc
body: <p>can i do it in linq to find parent-child relationship dynamically ?</p>
  
  <p>when user gives the child input,we have to find out the immediate parent and Top Level parent for the child.</p>
  
  <p>Schema:</p>
  
  <pre><code>**LocationId**   **LocationName**  **ParentId**
   1               InterNational          0
   2                National              1
   3                Regional              2
   4                SubRegional           3
   5                Area                  4
   6                City                  5
   7                Town                  6
   8              Municipality            7
  
  
  
  Input:When user type input as Area
  Outpt:immediate parent: SubRegional
  Top parent:International
  total parent:4
  
  if:town
  immediate parent:City
  top parent:international
  total parent:6
  </code></pre>
  
  <p>class</p>
  
  <pre><code>     public class ParentChild
          {
              public int LocationId { get; set; }
              public string LocationName { get; set; }
              public int parentId { get; set; }
  
              public static List&lt;ParentChild&gt; LocationParent()
              {
                  var s = new List&lt;ParentChild&gt;{
                  new ParentChild {LocationId=1,LocationName="InterNational",parentId=0},
                  new ParentChild {LocationId=2,LocationName=" National",parentId=1},
                  new ParentChild {LocationId=3,LocationName=" Regional ",parentId=2},
                  new ParentChild {LocationId=4,LocationName=" SubRegional",parentId=3},
                  new ParentChild {LocationId=5,LocationName="Area ",parentId=4},
                  new ParentChild {LocationId=6,LocationName=" City ",parentId=5},
                  new ParentChild {LocationId=7,LocationName="Town ",parentId=6},
                  new ParentChild {LocationId=8,LocationName="Municipality ",parentId=7}
                  };
  
                  return s;
  
              }
  
          }
  
  
          public class ParentChildViewModel()
          {
              public int LocationId { get; set; }
              public string LocationName { get; set; }
              public int parentId { get; set; }
              public string ParentName{get;set;}
              public int immediateparentId {get;set;}
              public string immediateparentName {get;set;}
  
  
          }
  </code></pre>
  
  <p>controller</p>
  
  <pre><code>   public ActionResult ParentChilds(string x)
          {
  
              var ss = from y in ParentChild.LocationParent()
                       where y.LocationName == x
                       select 
  -- How to do this in linq logic here?how i have write the logic
  
              return View(ss);
  
  
          }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model IEnumerable &lt;Dataclasses.ParentChildViewModel&gt;
  
  
  @{
      ViewBag.Title = "ParentChilds";
  }
  
  &lt;h2&gt;ParentChilds&lt;/h2&gt;
  
  @foreach (var x in Model)
  {
     &lt;p&gt;currentLocationName: @x.LocationName&lt;/p&gt;
      &lt;p&gt;currentLocationId :@x.LocationId&lt;/p&gt;
  
      &lt;p&gt;LocationTopParentId :@x.parentId&lt;/p&gt;
     &lt;p&gt;LocationTopParent:@x.ParentName&lt;/p&gt;
  
  &lt;p&gt;LocationimmediateparentId :@x.immediateparentId&lt;/p&gt;
     &lt;p&gt;LocationimmediateparentName:@x.immediateparentName&lt;/p&gt;
      &lt;br /&gt;
  
  }
  </code></pre>