开发者

Usercontrol to dispay fields from multiple LINQ tables

I have a number of LINQ to SQL tables with the same fields (Cr开发者_Go百科eatedDate 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;
}


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜