I have a collection of items coming from a database which has a parentid
value or null.
Here is my class design:
public class Item
{
public
Do you really need a setter for sub items? Also be mindful of the performance issues when you run Select* queries on SQL server.
public List- SubItems{
get
{
try{
var validParents = db.items.Where(x=>x.ParentId!=null && x.ParentId.Equals(Id)); //db is your dbcontext
if(validParents !=null)
{
return validParents.ToList();
}else
{
return null;
}
catch(Exception)
{
return null;
}
}
(Note: Think of adding this to your partial entity class. Never name your entity as "Item" :).Item is a reserved word. )