First, in your situation, models should look like this:
public class Product
{
public int id { get; set; }
public string name { get; set; }
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
}
public class Category
{
public int id { get; set; }
public string name { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual Category Parent { get; set; }
public int? ParentID { get; set; }
private List<int> GetIds(Category category)
{
var list = Products == null ? new List<int>() : Products.Select(x => x.id).ToList();
if (Categories != null)
Categories.ToList().ForEach(x => {
list.Add(x.id);
list.AddRange(x.GetIds(x));
}
);
return list;
}
public List<int> GetIds()
{
return GetIds(this);
}
}
And you will execute it this way:
var db = new DataContext();
//switching on "LazyLoading" is extremely necessary
db.Configuration.LazyLoadingEnabled = true;
var first = db.Categories.Where(x => x.id == 5).First();
var ids = first.GetIds();