简体   繁体   中英

Converting double[,] to a string and storing in SQL Server table column

I have a double array initialized as below

double[,] zDataDifference = new double[2048, 2048];

in order to store this data in a SQL server table column, i am trying to convert that to a string. Initially i thought of using the below code which I used to convert my double[] to string and was successful in doing so.

String.Join(",", NICorrectedMean.Select(p => p.ToString()).ToArray());

But since double[,] ie,(double[ , ]) does not have a definition for Select method and no extension method exists...

I do not want to use the below foreach loop as the whole application hangs due to large amount of data.

foreach(double dd in zDataRedDouble)
{
    ImageData += String.Join(",", dd);
}

Is there any quick or efficient way to convert the double[*,*] to a string in C#?

The reason your foreach solution is slow is because you are using String, and doing string concatenation for each iteration of the loop (4,194,304 iterations). By using a StringBuilder you could improve performance signicantly. If ease of use is of importance I would serialize this to json and store it that way, it also makes it very easy to reinitialize your array (you could also make it async so it doesn't slow down your application if nothing relies on it being added to the db first).

Foreach using StringBuilder (fastest way):

var ImageData = new StringBuilder();

foreach (var dd in zDataDifference)
{
   ImageData.Append(dd + ",");
}

Ease of use:

var ImageData = JsonConvert.SerializeObject(zDataDifference);

Try OfType<double>() in order to get IEnumerable<double> which you can put to Join :

double[,] zDataDifference = new double[2048, 2048];

...

string result = string.Join(",", zDataDifference.OfType<double>());

But be careful: 2048 * 2048 == 4194304 items means a lot of memory (and time)

Edit: StringBuilder solution (based on Kevin's answer)

   // In order NOT to reallocate the memory in the process, 
   // let's estimate imageData size in anvance.
   // Let us expect each item to be not more than of 20 characters
   var ImageData = new StringBuilder(2048 * 2048 * 20);

   foreach (var item in zDataDifference) {
     // We should add ',' before each item except the very first one
     if (ImageData.Length > 0)
       ImageData.Append(',');

     // Try to avoid creating a lot of small strings: item + ","
     ImageData.Append(item);
   }

   string result = ImageData.ToString();

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