在程序中,会根据查询语句得到相应的结果,形式如下: 
  string sql = "select * from TUser tu where tu.FUserId > 1";  // 查询实体
  IList items = GetObjects(sql); // 这个方法是自定义的
  经过这个查询,items中是一个个的object,这个时候,我要把object中的内容转换到一个字符串数组中,应该如何操作?
  
  如果查询语句如下:
  string sql = "select tu.FUserName, tu.FUserBirth from TUser tu where tu.FUserId > 1"; // 查询属性
  IList items = GetObjects(sql);
  这个时候,返回,items中是一个个的object[],可以通过下面代码输出其值内容:
  object[] rows = (object[])items[0];
  Console.WriteLine(rows[0] + "__" + rows[1]);
  朋友们,过来看看,出出主意

解决方案 »

  1.   

    string[] ss = new string[rows.GetLength(0)];
    int i;
    for (i = 0 ; i < row.GetLength(0); ++i)
    {
    ss[i] = rows[i].ToString();
    }
      

  2.   

    你的 GetObjects() 返回类型是什么?
      

  3.   

    GetObjects返回IList, System.Collections中的集合类型
      

  4.   

    JasonHeung(拥有一切也不过就这样笑着哭)  的代码反映的是第二种情形,即是查询属性的时候,结果返回ojbect[],这个时候可以获取object成员值内容.
      object[] rows = (object[])items[0];
      Console.WriteLine(rows[0] + "__" + rows[1]);   // all right
      

  5.   

    using System.Reflection;
    string sql = "select * from TUser tu where tu.FUserId > 1";  // 查询实体
    IList items = GetObjects(sql); // 这个方法是自定义的for ( int i = 0; i < items.Count; i++ )
    {
      if ( items[i].GetType().ToString() == "System.Object[]" )
      {
         object[] rows = (object[])items[i];
         string[] strTemp = new string[rows.GetLength(0)];
         int j = 0;
         while ( j < rows.GetLength(0) )
         {
            strTemp[j] = rows[j];
            j++;
         }
      }
      else
      {
         Type theType = items[i].GetType();
         PropertyInfo[] infoArray = theType.GetProperties();
         foreach ( PropertyInfo info in infoArray )
         {
            Console.WriteLin(info.GetValue(items[i], null));
         }
      }这段代码可以解决问题        
      

  6.   

    那么复杂干嘛,这样IList items = GetObjects(sql); // 这个方法是自定义的
    ArrayList ary = new ArrayList();
    ary.AddRange(items);//这样就转换完了
    string[] s = (string[])ary.ToArray(typeof(string));