How to get nested records from your object class List using linq.

Hello All,

Today we are going to explore on “how to get nested records from your object class List using linq.”

Here we will take a very small example of Category and SubCategory. We will first create 2 classes namely Category and SubCategory.

public class Category
{
            public int CategoryID { get; set; }
            public string CategoryName { get; set; }
            public List SubCategoryList { get; set; }
            public bool IsCategorychecked { get; set; }
}

public class SubCategory
{
            public int SubCategoryID { get; set; }
            public string SubCategoryName { get; set; }
            public bool IsSubCategorychecked { get; set; }
}

Lets fill the List object of Category.

List<Category> categoryList = new List<Category>();

Category category = new Category();

category.CategoryID = 1;

category.CategoryName = “Category One”;

SubCategory subCategory = new SubCategory();

subCategory.SubCategoryID = 1;

subCategory.SubCategoryName = “Sub Category One”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 2;

subCategory.SubCategoryName = “Sub Category Two”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 3;

subCategory.SubCategoryName = “Sub Category Three”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = true;

categoryList.Add(category);

category = new Category();

category.CategoryID = 2;

category.CategoryName = “Category Two”;

SubCategory subCategory = new SubCategory();

subCategory.SubCategoryID = 4;

subCategory.SubCategoryName = “Sub Category Four”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 5;

subCategory.SubCategoryName = “Sub Category Five”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 6;

subCategory.SubCategoryName = “Sub Category Six”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = false;

categoryList.Add(category);

category = new Category();

category.CategoryID = 3;

category.CategoryName = “Category Three”;

subCategory = new SubCategory();

subCategory.SubCategoryID = 7;

subCategory.SubCategoryName = “Sub Category Seven”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 8;

subCategory.SubCategoryName = “Sub Category Eight”;

subCategory.IsSubCategorychecked = true;

category.SubCategoryList.Add(subCategory);

subCategory = new SubCategory();

subCategory.SubCategoryID = 9;

subCategory.SubCategoryName = “Sub Category Nine”;

subCategory.IsSubCategorychecked = false;

category.SubCategoryList.Add(subCategory);

category.IsCategorychecked = true;

categoryList.Add(category);

Now if you want to get the selected Sub Category List for a particular category name so you can do the following query:

var query = (from item in _category
                   from nested in item.SubCategory
                   where nested.IsSubCategoryChecked.Equals(true)
                   select nested.SubCategoryName);

The above query will give result as:

Sub Category Two

Sub Category Three

Sub Category Seven

Sub Category Eight

Happy Coding!!!! :)