-1

I want to collect all child rows with a given parent from this table in TSQL.

ID     Name   parentID
------ ------ ---------
 1     ABCD          0
 2     xyz           1
 3     efg           1
 4     lm            2

Please let me know how can get in hierarchy list using linq.

Own
  • 174
  • 1
  • 11

2 Answers2

1

Make and AJAX call from the client to your static method:

          $.ajax({
              type: "POST",
              url: "/WebMethods/Test.aspx/getChildren",
              data: "{myID:" + someID+ "}",
              contentType: "application/json",
              dataType: "json",
              success: function (msg) {
                  // work on your json

              },
              error: function (msg) {
                  alert("error:" + JSON.stringify(msg));
              }
          });

Find your parent entity using a parameter, then you can find all children entities that belong to that parent. Make sure your foreign key constraints are established in your entity model.

       public static string getChildren(int myID){
                using (var rep = new context()) {
                Header head = rep.Headers.Where(x => x.ID== myID).First();

                var details = head.Details.ToList();
                }
       // Convert List to JSON and return to client
                string myChidren="";
                return myChildren;
       }
JustLearning
  • 3,164
  • 3
  • 35
  • 52
  • Nothing in the question indicates ASP.Net is involved, or that "client side" means a browser. –  Sep 14 '17 at 15:46
0

you can use this example for your case.

using (DbContext context = new DbContext())
{
    List<Collect> lstObj = context.Collect.OrderBy(c=> c.ID).ToList();
}
rdn87
  • 739
  • 5
  • 18