DataTable转换成List

本文详细介绍了Silverlight环境下通过Webservice访问数据库并将其查询结果转换为List<T>类型的实现过程,包括数据转换的代码实现及使用Nhibernate+Castle的案例分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的应用环境是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>,认知不深刻,还请大家多多发表建议,.....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值