locked
Extracting data fields for possible data in sub, sub-sub tables RRS feed

  • Question

  • User-157557353 posted

    I have a 3 level of table structure (L1, L2, L3) where L1 may or may not have a record in L2, which may or may not have records in L3. And there could be more than one record in L2 and L3 table linked to their parent table.

    Say:

    L1_table:

    ID, L1_fireld,

    L2_table:

    ID, L1_ID, L2 Field

    L3 table:

    ID, L2_ID, L3 Field

    I wonder what could be the option to extract all the data into a text string with  "|"  as a delimiter in Razor and ASP.net.

    for example:

    L1_table|ID value |L1_field value|

    or 

    L1_table|ID value |L1_field value|L2_table|L2_ID value| L2_Field value|...(L3_table|...)

    Any suggestions? 

    Thanks.

    Thursday, March 10, 2016 2:42 PM

Answers

  • User-2057865890 posted

    Hi eriche33,

    You could try the following code snippets.

    var menu = new List<Node>
    {
       new Node {Id = 4, ParentId = 1, Name = "aaa2"},
       new Node {Id = 5, ParentId = 4, Name = "aaa3"},
       new Node {Id = 6, ParentId = 2, Name = "bbb2"},
       new Node {Id = 7, ParentId = 6, Name = "bbb3"},
       new Node {Id = 8, ParentId = 3, Name = "ccc2"},
       new Node {Id = 9, ParentId = 8, Name = "cccc3"},
       new Node {Id = 1, ParentId = null, Name = "aaa"},
       new Node {Id = 2, ParentId = null, Name = "bbb"},
       new Node {Id = 3, ParentId = null, Name = "ccc"},
    };
    
    var s = new StringBuilder();
    var ms = MenuString(menu, s);
    var r = ms.ToString();
    
    
    
    public static string MenuString(List<Node> menu, StringBuilder sb, int? parentid = null, int level = 0)
            {
                var items = menu.Where(m => m.ParentId == parentid);
    
                if (items.Any())
                {
                    if (items.First().ParentId.HasValue)
                    {
                        level++;
                    }
                    foreach (var item in items)
                    {
                        if (level > 0)
                        {
                            sb.Append("|");
                        }
                        sb.Append(item.Name);
                        MenuString(menu, sb, item.Id, level);
                    }
                }
    else
                {
                    sb.Append("|");
                } return sb.ToString(); }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 14, 2016 9:12 AM

All replies

  • User-2057865890 posted

    Hi eriche33,

    You could try the following code snippets.

    var menu = new List<Node>
    {
       new Node {Id = 4, ParentId = 1, Name = "aaa2"},
       new Node {Id = 5, ParentId = 4, Name = "aaa3"},
       new Node {Id = 6, ParentId = 2, Name = "bbb2"},
       new Node {Id = 7, ParentId = 6, Name = "bbb3"},
       new Node {Id = 8, ParentId = 3, Name = "ccc2"},
       new Node {Id = 9, ParentId = 8, Name = "cccc3"},
       new Node {Id = 1, ParentId = null, Name = "aaa"},
       new Node {Id = 2, ParentId = null, Name = "bbb"},
       new Node {Id = 3, ParentId = null, Name = "ccc"},
    };
    
    var s = new StringBuilder();
    var ms = MenuString(menu, s);
    var r = ms.ToString();
    
    
    
    public static string MenuString(List<Node> menu, StringBuilder sb, int? parentid = null, int level = 0)
            {
                var items = menu.Where(m => m.ParentId == parentid);
    
                if (items.Any())
                {
                    if (items.First().ParentId.HasValue)
                    {
                        level++;
                    }
                    foreach (var item in items)
                    {
                        if (level > 0)
                        {
                            sb.Append("|");
                        }
                        sb.Append(item.Name);
                        MenuString(menu, sb, item.Id, level);
                    }
                }
    else
                {
                    sb.Append("|");
                } return sb.ToString(); }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 14, 2016 9:12 AM
  • User-157557353 posted

    Will check it out. Thanks Chris!

    Friday, March 25, 2016 1:49 PM