Tipx Tipx - 6 months ago 46
C# Question

Custom Generic GetTable

For a DataContext I'm working on, I don't want to load the Tables untill their needed.
I want to make a little method that would check if a certain Table is loaded before loading it, but I end up having n Methods doing the same thing :

private void Load(ref Table<Order> Orders)
if (Orders == null)
Orders = this.GetTable<Order>();

I'm trying to make a generic one for obvious reasons, but I get a "The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table'" exception while doing this :

private void Load<T>(ref Table<T> TableToLoad)
if (TableToLoad == null)
TableToLoad = this.GetTable<T>();


Simply add 'where T: class' to your method.