Get Parent of Child with Linq using C# and VB.Net in ASP.Net

Last Reply 23 days ago By arunkurmi

Posted 25 days ago

hi

How to get parent category when pass child.

i have this same table which has parent child relation

i want linQ to query data so if i pass Category_id it retruns main first parent_category_id in below case if filter category_id = 16 it should return parent_categoyry_id =1 

bcs 16 is child of categoryid 4 and categoryid 4 is chld of category id 1

please advice

category_id category_name parent_category_id

1 animal NULL

2 vegetable NULL

3 fruit NULL

4 doggie 1

5 horsie 1

6 birdie 1

7 carrot 2

8 leafy 2

9 potato 2

10 celery 10

11 rutabaga 2

12 mango 3

13 orange 3

14 graps 3

15 apple 3

16 companion 4

17 herding 4

18 poodle 19

19 chihuahua 19

20 shepherd 20

21 collie 20

You are viewing reply posted by: arunkurmi 23 days ago.
Posted 23 days ago

Hi nauna,

Check this sample. now take its reference.

HTML

 <asp:TextBox runat="server" ID="txtCategoryID" AutoPostBack="true" OnTextChanged="OnChanged" />

Code

C#

protected void OnChanged(object sender, EventArgs e)
{
    Data result = GetParent(GetData(), txtCategoryID.Text);
    if (result != null)
    {
        while (result.Parent_Category_Id != "NULL")
        {
            if (result.Parent_Category_Id == result.Category_ID)
            {
                result = GetParent(GetData(), result.Parent_Category_Id);
                break;
            }
            else
            {
                result = GetParent(GetData(), result.Parent_Category_Id);
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + result.Category_Name + "')", true);
    }
    else
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('CategoryID does not Exist!')", true);
    }
}

private Data GetParent(List<Data> data, string parentId)
{
    return data.Where(x => x.Category_ID == parentId).FirstOrDefault();
}

private List<Data> GetData()
{
    List<Data> dt = new List<Data>();
    dt.Add(new Data { Category_ID = "1", Category_Name = "Animal", Parent_Category_Id = "NULL" });
    dt.Add(new Data { Category_ID = "2", Category_Name = "Vegetable", Parent_Category_Id = "NULL" });
    dt.Add(new Data { Category_ID = "3", Category_Name = "Fruit", Parent_Category_Id = "NULL" });
    dt.Add(new Data { Category_ID = "4", Category_Name = "Doggie", Parent_Category_Id = "1" });
    dt.Add(new Data { Category_ID = "5", Category_Name = "Horsie", Parent_Category_Id = "1" });
    dt.Add(new Data { Category_ID = "6", Category_Name = "Birdie", Parent_Category_Id = "1" });
    dt.Add(new Data { Category_ID = "7", Category_Name = "Carrot", Parent_Category_Id = "2" });
    dt.Add(new Data { Category_ID = "8", Category_Name = "Leafy", Parent_Category_Id = "2" });
    dt.Add(new Data { Category_ID = "9", Category_Name = "Potato", Parent_Category_Id = "2" });
    dt.Add(new Data { Category_ID = "10", Category_Name = "Celery", Parent_Category_Id = "10" });
    dt.Add(new Data { Category_ID = "11", Category_Name = "Rutabaga", Parent_Category_Id = "2" });
    dt.Add(new Data { Category_ID = "12", Category_Name = "Mango", Parent_Category_Id = "3" });
    dt.Add(new Data { Category_ID = "13", Category_Name = "Orange", Parent_Category_Id = "3" });
    dt.Add(new Data { Category_ID = "14", Category_Name = "Graps", Parent_Category_Id = "3" });
    dt.Add(new Data { Category_ID = "15", Category_Name = "Apple", Parent_Category_Id = "3" });
    dt.Add(new Data { Category_ID = "16", Category_Name = "Companion", Parent_Category_Id = "4" });
    dt.Add(new Data { Category_ID = "17", Category_Name = "Herding", Parent_Category_Id = "4" });
    dt.Add(new Data { Category_ID = "18", Category_Name = "Poodle", Parent_Category_Id = "19" });
    dt.Add(new Data { Category_ID = "19", Category_Name = "Chihuahua", Parent_Category_Id = "19" });
    dt.Add(new Data { Category_ID = "20", Category_Name = "Shepherd", Parent_Category_Id = "20" });
    dt.Add(new Data { Category_ID = "21", Category_Name = "Collie", Parent_Category_Id = "20" });

    return dt;
}

public class Data
{
    public string Category_ID { get; set; }
    public string Category_Name { get; set; }
    public string Parent_Category_Id { get; set; }
}

VB.Net

Protected Sub OnChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim result As Data = GetParent(GetData(), txtCategoryID.Text)
    If result IsNot Nothing Then
        While result.Parent_Category_Id <> "NULL"

            If result.Parent_Category_Id = result.Category_ID Then
                result = GetParent(GetData(), result.Parent_Category_Id)
                Exit While
            Else
                result = GetParent(GetData(), result.Parent_Category_Id)
            End If
        End While
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & result.Category_Name & "')", True)
    Else
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('CategoryID does not Exist!')", True)
    End If
End Sub

Private Function GetParent(ByVal data As List(Of Data), ByVal parentId As String) As Data
    Return data.Where(Function(x) x.Category_ID = parentId).FirstOrDefault()
End Function

Private Function GetData() As List(Of Data)
    Dim dt As List(Of Data) = New List(Of Data)()
    dt.Add(New Data With {.Category_ID = "1", .Category_Name = "Animal", .Parent_Category_Id = "NULL"})
    dt.Add(New Data With {.Category_ID = "2", .Category_Name = "Vegetable", .Parent_Category_Id = "NULL"})
    dt.Add(New Data With {.Category_ID = "3", .Category_Name = "Fruit", .Parent_Category_Id = "NULL"})
    dt.Add(New Data With {.Category_ID = "4", .Category_Name = "Doggie", .Parent_Category_Id = "1"})
    dt.Add(New Data With {.Category_ID = "5", .Category_Name = "Horsie", .Parent_Category_Id = "1"})
    dt.Add(New Data With {.Category_ID = "6", .Category_Name = "Birdie", .Parent_Category_Id = "1"})
    dt.Add(New Data With {.Category_ID = "7", .Category_Name = "Carrot", .Parent_Category_Id = "2"})
    dt.Add(New Data With {.Category_ID = "8", .Category_Name = "Leafy", .Parent_Category_Id = "2"})
    dt.Add(New Data With {.Category_ID = "9", .Category_Name = "Potato", .Parent_Category_Id = "2"})
    dt.Add(New Data With {.Category_ID = "10", .Category_Name = "Celery", .Parent_Category_Id = "10"})
    dt.Add(New Data With {.Category_ID = "11", .Category_Name = "Rutabaga", .Parent_Category_Id = "2"})
    dt.Add(New Data With {.Category_ID = "12", .Category_Name = "Mango", .Parent_Category_Id = "3"})
    dt.Add(New Data With {.Category_ID = "13", .Category_Name = "Orange", .Parent_Category_Id = "3"})
    dt.Add(New Data With {.Category_ID = "14", .Category_Name = "Graps", .Parent_Category_Id = "3"})
    dt.Add(New Data With {.Category_ID = "15", .Category_Name = "Apple", .Parent_Category_Id = "3"})
    dt.Add(New Data With {.Category_ID = "16", .Category_Name = "Companion", .Parent_Category_Id = "4"})
    dt.Add(New Data With {.Category_ID = "17", .Category_Name = "Herding", .Parent_Category_Id = "4"})
    dt.Add(New Data With {.Category_ID = "18", .Category_Name = "Poodle", .Parent_Category_Id = "19"})
    dt.Add(New Data With {.Category_ID = "19", .Category_Name = "Chihuahua", .Parent_Category_Id = "19"})
    dt.Add(New Data With {.Category_ID = "20", .Category_Name = "Shepherd", .Parent_Category_Id = "20"})
    dt.Add(New Data With {.Category_ID = "21", .Category_Name = "Collie", .Parent_Category_Id = "20"})
    Return dt
End Function

Public Class Data
    Public Property Category_ID As String
    Public Property Category_Name As String
    Public Property Parent_Category_Id As String
End Class

Screenshot