I am using telerik's OpenAccess ORM and I am making some changes to the generated code. For an entity that has a navigation property (a key to another table), these are generated as ILists like the following:
private IList<SystemUser> _SystemUsers = new List<SystemUser>();
public virtual IList<SystemUser> SystemUsers
{
get
{
return this._SystemUsers;
}
}
First, at what point is SystemUsers
actually queried against the database? It is an IList, so I would have thought that this would be executed against the database on object creation, but I know that isn't the case.
What I want to do is filter out deleted items on all my generated navigation properties and I have been making changes to the t4 template to do this with the following code:
private IList<SystemUser> _SystemUsers = new List<SystemUser>();
public virtual IList<SystemUser> SystemUsers
{
get
{
if (!Entities.IncludeDeletedEntities)
{
var currentContext = Entities.GetContext(this);
ContextChanges changes = currentContext.GetChanges();
IList<SystemUser> deletedItems = changes.GetDeletes<SystemUser>();
return this._SystemUsers.Except(deletedItems).ToList(); //Question is here
}
return this._SystemUsers;
}
}
Essentially this just returns the collection minus the ones that are marked for deletion. My concern is about the .ToList()
and when it is executed. I don't want to slow my queries down by causing that ToList()
to query against the database every time I access SystemUsers
, but I also want to filter my items. I'm not even sure if that ToList()
would cause a database hit. I'm not sure when the code would actually hit the database, so I am looking looking for some help/tips to filter out my deleted items without it hitting the database until I use SystemUsers
in my code by either adding further filters (where clauses, etc).