如何从对象数组中解析数据

时间:2016-04-12 09:40:00

标签: c# arrays object indexing sqldatareader

我有从数据库读取数据的iienumerable方法,这个数据放入对象数组

foreach (var items in repo.ExecuteReader()
   {
        rss.Id = items[0]; // not work
        yield return items;
   }

另一种方法是迭代这些对象

// something before this ...
// filename_list is quite long, 100k+ files

vector<string> some_string_list(filename_list.size(), " ");
int file_count = filename_list.size();

int counter = 0;

#pragma omp parallel for reduction(+:counter) schedule(auto)  
for (int i = 0; i < file_count; i++)
{
  string loop_filename = filename_list[i];  
  counter++;

  // The filename is used to call a class member function and get some data, the file is used for reading
  // This is the heavy part of code
  some_string_list[i] = someclassinstance.getSomeString(loop_filename, param1, param2);
  int temp = counter;
  if(temp % 1000 == 0)
  {
    cout << "." << flush;
  }
}

cout << endl;

如何索引那些&#39;项目数组?

3 个答案:

答案 0 :(得分:1)

SqlDataReader reader = repo.ExecuteReader();
while (reader.reas.Read()) {
       rss.Id = (int)reader[0]; // should work

更新

试试这个

在将ExecuteReader() 方法分配给对象或变量之前,{{1}} 方法的结果必须取消装箱(看起来像是转换,但这是拆箱)到正确的类型。 / p>

Are you asking why?

答案 1 :(得分:1)

首先尝试投射,见下文:

foreach (var items in repo.ExecuteReader()
   {
        var obj[] = (object[])items;
        rss.Id = obj[0];
        yield return obj;
   }

答案 2 :(得分:0)

解决方案由 Hari Prasad 提供,只需要从

更改
   foreach (var items in repo.ExecuteReader()) {} 

   foreach (var object[] items in repo.ExecuteReader()

现在索引和强制转换正在运行