Let's say I have this class Location, which represents locations.
public class Location
{
public int LocationID { get; set; }
public int JuridictionID { get; set; }
public int LocationName { get; set; }
}
Exemple: Silver Spring is located in Montgomery County, which is located in Maryland, Which is located in the USA. Thus, JuriditionID for Maryland would be the the LocationID for the USA, and so on.
public List<Location> SetHierarchyOfLocation(int JuridicitionID)
{
var list = new List<Location>();
if (JuridicitionID > 0)
{
while (JuridicitionID > 0)
{
var location = new Location();
location = GetLocationByID(JuridicitionID);
list.Add(location);
JuridicitionID = location.JuridictionID;
}
}
list.Reverse();
return list;
}
pubilc Location GetLocationByID(int LocationID)
{
return db.Locations.FirstOrDefault(l => l.LocationID == LocationID);
}
This works perfectly. But, now I realize that it will result in a lot of single calls to the database.
But I don't know how to write a stored procedure that will replace this. Can someone help me???
Thanks.