Aleena Saviour

Aleena Saviour

  • NA
  • 519
  • 87.7k

select subcategory list inside category list

Jul 18 2018 12:52 AM
this is my query for selecting categories and subcategories.
List<Category> lstcat = new List<Category>();
var categ = _db.Category
.Select(c => new Category()
{
PKCatId = c.PKCatId,
CatName = c.CatName,
Icon = c.Icon,
ActiveIcon = c.ActiveIcon,
Subcat = GetChildren(_db, c.PKCatId)
})
.ToList(); 
 
public static List<SubCat> GetChildren(HomeDBContext _db, int parentId)
{
return
_db.subCat.Where(c => c.PKCatId == parentId)
.Select(c => new SubCat
{
PKSubCatId = c.PKSubCatId,
SubCatName = c.SubCatName,
PKCatId = c.PKCatId
})
.ToList();
It gives error like "The entity or complex type 'EcomHomeFurniture.Models.Category' cannot be constructed in a LINQ to Entities query."
can anyone help me to solve this? 
 

Answers (2)