0

How to store a double array in one column (field) of a DataTable?

I want to have 2 data fields to keep in a row, the first one being an integer which is the ID of the product and the second one being a double array which contains all the different prices the product has had (evolution of the product's prices). How can I store these 365 prices of the product in the same row. Maybe I should use an array price:

dtPriceOfProduct.Rows.Add(product_id, price)

where price is an array of all the prices the product with product_id can have. I want to have 1x5,000 rows not 365x5,000.

Currently I am storing data like this

int product_id;
double[] price = new double[];

and I create a new row in my DataTable for each price of the same product

foreach(double price_data in price)
{
    dtPriceOfProduct.Rows.Add(product_id, price_data)
}
silver est
  • 51
  • 6
John R
  • 63
  • 7
  • Could you please clarify this line? `How can I store these 365 prices of the product in the same row` so you want to create a table with `365 columns` and a single row? or you want to list the whole array in a single cell? – sujith karivelil Jun 20 '16 at 04:07

1 Answers1

0

How can i store the array for the product into the same row.

IMO what you want to achieve is not a good idea. But if you insist that you still want to continue with that design, I suggest you use a dictionary with the item id as the key and the array as the value*. Then serialize the said dictionary before saving into the said DataTable.

*something like this perhaps?

Dictionary<int, double[]> priceHistory = new Dictionary<int, double[]>();
priceHistory.Add(id, price);

for the serialization/deserialization, kindly refer here

Community
  • 1
  • 1
terrible-coder
  • 323
  • 2
  • 9
  • 18
  • If this is a bad idea, what do you suggest me? I have to use a datable because, after, i use data into ReportViewer to display the price's evolution – John R Jun 19 '16 at 08:57
  • What I could suggest right now is do pivoting on `DataTable` (i.e. Have multiple rows with the same id and perhaps different day and price.) You may see [this](http://stackoverflow.com/questions/11624676/database-pivoting-what-is-the-purpose) for more information. I also believe this will work on `DataTables` too. You may have to do querying on your reports, but it's basically the same thing. – terrible-coder Jun 19 '16 at 09:03
  • You should really be using a related table for your prices. A google search will easily find plenty of examples and advice. Here's something to get you started: https://chsakell.com/2013/06/22/ado-net-working-with-dataset-datatable-datacolumn-datarow-and-datarelations/ – Phil Blackburn Jun 19 '16 at 09:12