We have a C # application that populates tables on sheets in an Excel document.
Tables should be populated in the order in which rows are returned from the database.
The DataFileColData object is defined as a List and contains the rows of the result set. For testing purposes, I use only [0] from the list.
The code segment 1 below does not work. The order of the lines is not preserved in the fact that in the final result the data are displayed out of order, although the numbers themselves are listed in order:
if (DataFileColData[0].Count() > 0)
{
ConcurrentDictionary<int, DataRow> theRows = new ConcurrentDictionary<int, DataRow>(9, DataFileColData[0].Count());
Parallel.For(0, DataFileColData[0].Count(), i =>
{
int c = 0;
try
{
foreach (var Col in DataFileColData)
{
var cell = Col[i];
if (cell != null)
{
if (cell.GetType().Name == "JArray")
{
if (theRows.TryAdd(i, Dt.NewRow()))
theRows[i].ItemArray = JsonConvert.DeserializeObject<object[]>(Col[i].ToString());
}
else
{
if (theRows.TryAdd(i, Dt.NewRow()))
theRows[i][c] = cell;
}
}
c++;
}
}
catch (Exception e)
{
throw new Exception("Exception thrown in \"PublicMethods.cs | RenderExcelFile\" while in foreach loop over DataFileColData: " + e.ToString());
}
}
);
for (int x = 0; x < theRows.Count; x++)
Dt.Rows.Add(theRows[x]);
Dt.TableName = ExcelTableSpec.TableTitle + " " + r.TableID;
}
code segment No. 2 below works with line order and data associated with each stored line:
if (DataFileColData[0].Count() > 0)
{
DataRow[] theRows = new DataRow[DataFileColData[0].Count()];
Parallel.For(0, DataFileColData[0].Count(), i =>
{
DataRow Rw = Dt.NewRow();
int c = 0;
try
{
foreach (var Col in DataFileColData)
{
var cell = Col[i];
if (cell != null)
{
if (cell.GetType().Name == "JArray")
{
lock (theRows)
{
theRows[i] = Dt.NewRow();
theRows[i].ItemArray = JsonConvert.DeserializeObject<object[]>(Col[i].ToString());
}
}
else
{
lock (theRows)
{
theRows[i] = Dt.NewRow();
theRows[i][c] = cell;
}
}
}
c++;
}
}
catch (Exception e)
{
throw new Exception("Exception thrown in \"PublicMethods.cs | RenderExcelFile\" while in foreach loop over DataFileColData: " + e.ToString());
}
}
);
Dt = theRows.CopyToDataTable();
Dt.TableName = ExcelTableSpec.TableTitle + " " + r.TableID;
}
, . , , "i", ConcurrentDictionary .
- , , , ?
!
@Enigmativity .
MSDN ( ), , , DataTable, MSDN , NewRow().
:
if (DataFileColData[0].Count() > 0)
{
DataRow[] theRows = new DataRow[DataFileColData[0].Count()];
Parallel.For(0, DataFileColData[0].Count(), i =>
{
lock (Dt)
{
theRows[i] = Dt.NewRow();
}
int c = 0;
try
{
foreach (var Col in DataFileColData)
{
var cell = Col[i];
if (cell != null)
{
if (cell.GetType().Name == "JArray")
{
theRows[i].ItemArray = JsonConvert.DeserializeObject<object[]>(Col[i].ToString());
}
else
{
theRows[i][c] = cell;
}
}
c += 1;
}
}
catch (Exception e)
{
throw new Exception("Exception thrown in \"PublicMethods.cs | RenderExcelFile\" while in foreach loop over DataFileColData: " + e.ToString());
}
}
);
Dt = theRows.CopyToDataTable();
Dt.TableName = ExcelTableSpec.TableTitle + " " + r.TableID;
if (theRows != null)
Array.Clear(theRows, 0, theRows.Length);
theRows = null;
}