我有许多具有相同字段的LINQ to SQL表(CreatedDate
和CreatedUser
)。
如果数据以IQueryable<T>
或IEnumerable<T>
例如:
public void UpdateStatus<T>(IQueryable<T> data)
{
DateTime? theDate = data.Single().CreatedDate; // doesn't work
string theUser = data.Single().CreatedUser;
}
答案 0 :(得分:0)
在usercontrol中为所需属性定义接口,然后将泛型方法限制为实现接口的类型:
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;
}
或者,完全删除通用位:
public void UpdateStatus(IQueryable<IStatusInfo> data) {
IStatusInfo item = data.Single();
DateTime? theDate = item.CreatedDate;
string theUser = item.CreatedUser;
}