如何在Mvc中动态使用Linq查找递归父子层次结构

时间:2015-11-13 19:49:49

标签: asp.net-mvc linq linq-to-sql

我可以在linq中动态查找父子关系吗?

当用户给出孩子输入时,我们必须找出孩子的直接父母和顶级父母。

架构:

**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

     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;}


        }

控制器

   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);


        }

查看:

@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 />

}

2 个答案:

答案 0 :(得分:1)

可重用的东西可能看起来像这样

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;
        }
    }

对于您的具体情况,它们将被用作

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 );

答案 1 :(得分:0)

我使用linq将数据放入字典中,然后进行简单的搜索和查找。

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; }
}