Tipx Tipx - 1 year ago 128
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>();

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download