views:

23

answers:

1

I have a number of LINQ to SQL tables with the same fields (CreatedDate and CreatedUser). I would like a usercontrol to display the values from these fields if the data is passed in as an IQueryable<T> or IEnumerable<T>

For example:

public void UpdateStatus<T>(IQueryable<T> data)
{
    DateTime? theDate = data.Single().CreatedDate;  // doesn't work 
    string theUser = data.Single().CreatedUser;
}
A: 

Define an interface for the properties you want in the usercontrol, then restrict the generic method to types that implement the interface:

interface IStatusInfo {
    DateTime? CreatedDate { get; }
    string CreatedUser { get; }
}

public void UpdateStatus<T>(IQueryable<T> data) where T : IStatusInfo {
    T item = data.Single();
    DateTime? theDate = item.CreatedDate;
    string theUser = item.CreatedUser;
}

Or alternatively, drop the generic bit altogether:

public void UpdateStatus(IQueryable<IStatusInfo> data) {
    IStatusInfo item = data.Single();
    DateTime? theDate = item.CreatedDate;
    string theUser = item.CreatedUser;
}
Christian Hayter