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?

2
  • 3
    Possible duplicate of reading an array column in C# Commented Sep 5, 2016 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. Commented Sep 5, 2016 at 23:07

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I am using this idea List<double[]> arr1 = new List<double[]>(); foreach (DataRow row in ds.Tables["test"].Rows) { arr1.Add(row["arr"].Select(s => Double.Parse(s)).ToList()); } but, I got error!!
Error 2 'System.Linq.Queryable.Select<TSource,TResult>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TResult>>)' is a 'method', which is not valid in the given context C:\Users\PCLORD\Desktop\ShowImg\ShowImg\Default.aspx.cs 103 39 ShowImg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.