6

I am using C# with postgresql. In the database I have a table named test and in this table I have a column named arr that it's datatype is double[] and I stored multiple record like this {1, 1, 2, 3, 0, 5, 1, 4}. Now, how to return those records into C# program and stored in a list for example List<double[]> arr1 = new List<double[]>();

Who can help me?

zana
  • 126
  • 2
  • 8
  • 3
    Possible duplicate of [reading an array column in C#](http://stackoverflow.com/questions/18654994/reading-an-array-column-in-c-sharp) – Crozin Sep 05 '16 at 11:33
  • You should add your code then we can help you, because there are a dozen of ways to do this, e.g. using Linq, running query statements directly, getting data from a service provider, etc. – Mohsen Kamrani Sep 05 '16 at 23:07

2 Answers2

5

For array datatypes, all you have to do is cast the result as an array of double:

NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();

NpgsqlCommand cmd = new NpgsqlCommand("select arr from test", conn);
NpgsqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
    double[] myArray = (double[])reader.GetValue(0);
    // do your bidding
}

reader.Close();

-- EDIT 2/18/2021 --

As of Npgsql 5.0, the above method seems to work most of the time, but the documents specify a different way to extract array datatypes:

double[] myArray = reader.GetFieldValue<double[]>(0)
Hambone
  • 15,600
  • 8
  • 46
  • 69
0

Here is a walkthrough, Using PostgreSQL in your C# (.NET): Using PostgreSQL in your C# (.NET)

You will need to give us some specific problems that you are having so we can help solve the problem.

Vural
  • 8,666
  • 11
  • 40
  • 57
  • I am using this idea `List arr1 = new List(); foreach (DataRow row in ds.Tables["test"].Rows) { arr1.Add(row["arr"].Select(s => Double.Parse(s)).ToList()); }` but, I got error!! – zana Sep 05 '16 at 11:40
  • Error 2 'System.Linq.Queryable.Select(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' is a 'method', which is not valid in the given context C:\Users\PCLORD\Desktop\ShowImg\ShowImg\Default.aspx.cs 103 39 ShowImg – zana Sep 05 '16 at 12:58