Reputation: 2543
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 />
}
Upvotes: 0
Views: 1289
Reputation: 1621
Something reusable might look like this
public static class MyExtensions
{
public static List<T> Parents<T>(this List<T> list, T current, Func<T, int> getId, Func<T, int> getPid)
{
List<T> returnlist = new List<T>();
T temp = list.FirstOrDefault(x => getPid(current) == getId(x));
while (temp != null)
{
returnlist.Add(temp);
current = temp;
temp = list.FirstOrDefault(x => getPid(current) == getId(x));
}
return returnlist;
}
}
and for your specific case it would them be used as
var locations = ParentChild.LocationParent();
var parents = locations.Parents(locations[6], x => x.LocationId, x => x.parentId);
Console.WriteLine( "Immediate Parent {0}, top - parent:{1}, count : {2}",
parents.First().LocationName,
parents.Last().LocationName,
parents.Count );
Upvotes: 2
Reputation: 70523
I'd use linq to put the data in a dictionary, then it is a simple search and lookup.
void Main()
{
List<loc> basedata = new List<loc>()
{
new loc() { ID = 1, name = "InterNational", pID = 0 },
new loc() { ID = 2, name = "National", pID = 1 },
new loc() { ID = 3, name = "Regional", pID = 2 },
new loc() { ID = 4, name = "SubRegional", pID = 3 },
new loc() { ID = 5, name = "Area", pID = 4 },
new loc() { ID = 6, name = "City", pID = 5 },
new loc() { ID = 7, name = "Town", pID = 6 },
new loc() { ID = 8, name = "Municipality", pID = 7 },
};
Dictionary<int,loc> data = basedata.ToDictionary(x => x.ID);
string test1 = "Area";
loc test1parent = data[data.First(x => x.Value.name == test1).Value.pID];
loc top1;
for(top1 = data.First(x => x.Value.name == test1).Value;top1.pID!=0;top1 = data[top1.pID]);
Console.WriteLine("Immediate Parent = "+test1parent.name);
Console.WriteLine("Top parent = "+top1.name);
string test2 = "Town";
loc test2parent = data[data.First(x => x.Value.name == test2).Value.pID];
loc top2;
for(top2 = data.First(x => x.Value.name == test2).Value;top2.pID!=0;top2 = data[top2.pID]);
Console.WriteLine("Immediate Parent = "+test2parent.name);
Console.WriteLine("Top parent = "+top2.name);
}
public class loc
{
public int ID { get; set; }
public string name { get; set; }
public int pID { get; set; }
}
Upvotes: 0