can i do it in linq to find parent-child relationship dynamically ?
when user gives the child input,we have to find out the immediate parent and Top Level parent for the child.
Schema:
**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
class
public class ParentChild
{
public int LocationId { get; set; }
public string LocationName { get; set; }
public int parentId { get; set; }
public static List<ParentChild> LocationParent()
{
var s = new List<ParentChild>{
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;}
}
controller
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);
}
View:
@model IEnumerable <Dataclasses.ParentChildViewModel>
@{
ViewBag.Title = "ParentChilds";
}
<h2>ParentChilds</h2>
@foreach (var x in Model)
{
<p>currentLocationName: @x.LocationName</p>
<p>currentLocationId :@x.LocationId</p>
<p>LocationTopParentId :@x.parentId</p>
<p>LocationTopParent:@x.ParentName</p>
<p>LocationimmediateparentId :@x.immediateparentId</p>
<p>LocationimmediateparentName:@x.immediateparentName</p>
<br />
}
Parenton yourParentChildclass, in which you can use to reference the direct parent.