简体   繁体   中英

OleDbDataAdapter into list of doubles C#

Is it possible parsing OleDbDataAdapter object into list of double if that object contains only one Column shown on a picture below?

I want to parse values from excel file and after that I want to check all values in list with foreach and if loops. Can someone help me with going through OleDbDataAdapter or even better converting it into list of doubles.

Column with values that are in OleDbDataAdapter object looks like this. It has few empty fields and bunch of double values.

在此处输入图片说明

OleDbDataAdapter is define like this:

string pathConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection connection = new OleDbConnection(pathConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select [max t] from [DAYTIME CONFORT INDEX$]", connection);

It seems more convenient to use an OleDbDataReader in this context

 List<double> numbers = new List<double>();
 string pathConnection = "....";
 using(OleDbConnection connection = new OleDbConnection(pathConnection))
 using(OleDbCommand cmd = new OleDbCommand("Select [max t] from [DAYTIME CONFORT INDEX$]", connection))
 {
      connection.Open();
      using(OleDbDataReader reader = cmd.ExecuteReader())
      {
          while(reader.Read())
          {
             if(!reader.IsDBNull(0))
                 numbers.Add(Convert.ToDouble(reader[0]));
          }
      }
 }

With an OleDbDataAdapter you are forced to load a DataTable/Dataset to retrieve your values and then loop again over the datatable to transform the single column in a list of double

DataTable dt =new DataTable();;

myDataAdapter.Fill(dt);

List<double> numbers= dt.AsEnumerable()
                         .Select(r => r.Field<double?>("[max t]"))
                         .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM