我的应用环境是silverlight+webservice访问数据库,因silverlight的datagrid只能绑定List<T>类型(我最上面的问题其实似乎有点不对题),因此我想把查询的数据转换为List<T>类型:
public List<T> Query<T>(string viewName, string listName, string groupby, string orderbyName, string filter)
{
DataTable table = Query(viewName, listName, groupby, orderbyName, filter);
IList<T> list = new List<T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance<T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
if (!pro.CanWrite) continue;
object value = row[tempName];
if (value != DBNull.Value)
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list.ToList();
}
webservice的方法封装如下:
/// <summary>
/// 查询系统用户列表
/// </summary>
/// <returns></returns>
[WebMethod(Description = "查询系统用户列表")]
public List<SystemUser> GetSystemUserList(string viewName, string listName, string groupby, string orderbyName, string filter)
{
IViewService viewService = CastleContainer.Resolve<IViewService>();<--此处我使用了Nhibernate+Castle的内容,可以忽略,调用方法雷同
return viewService.Query<SystemUser>(viewName, listName, groupby, orderbyName, filter);
}
另外增加说一句,如果使用O/R Mapping的话,HQL写出来似乎可以直接转List<T>,认知不深刻,还请大家多多发表建议,.....
DataTable转换成List
最新推荐文章于 2023-11-29 20:44:30 发布