gpt4 book ai didi

c# - 将数组添加到列数有限的数据表中

转载 作者:行者123 更新时间:2023-11-30 16:38:19 27 4
gpt4 key购买 nike

我已经知道如何将数组添加到 datatable 中。

string[] months = {"Jan" , "Feb" , "Mar" , "Apr" , "Jun"};
DataTable dt = new DataTable();
dt.Columns.Add("M1");
dt.Columns.Add("M2");
dt.Columns.Add("M3");
dt.Columns.Add("M4");
dt.Columns.Add("M5");
DataRow row = dt.NewRow();

for (int i = 0; i < months.Length; i++)
{
row[i] = months[i];
}

dt.Rows.Add(row);

以上代码完美运行。但我有一个不同的问题。假设我有一个包含 8 个值的数组。

string[] arr = {"1","2","3","4","5","6","7","8"};

现在我已经创建了一个包含 4 列的新 datatable

DataTable dt = new DataTable();

dt.Columns.Add("v1");
dt.Columns.Add("v2");
dt.Columns.Add("v3");
dt.Columns.Add("v4");

DataRow row = dt.NewRow();

现在我想将数组的值添加到像这样的列中

arr[0] = v1
arr[1] = v2
arr[2] = v3
arr[3] = v4

arr[4] = v1
arr[5] = v2
arr[6] = v3
arr[7] = v4

更新 1

我有一个函数,我想在其中执行所有这些操作

public string LoadAMIReadings(string startTS, string endTS, string batch, string divCode, string cc_code)
{

totalRec = 0;
processedRec = 0;
this.BATCH = batch;
this.DIVCODE = divCode;
this.CCCODE = cc_code;
this.FROMDATE = startTS;
this.TODATE = endTS;
HESservicesDoCommandRequest obj = new HESservicesDoCommandRequest();




DataTable dt = new DataTable();
dt.Columns.Add("Application_No", typeof(string));
dt.Columns.Add("REF_NO", typeof(string));
dt.Columns.Add("METER_SERIAL_NO", typeof(string));
dt.Columns.Add("XMETER_NO", typeof(string));

// here I want to call the modified solution

int rowCount = bill.Length / dt.Columns.Count; // array bill is declared globally with 7780 values in it
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
DataRow row = dt.NewRow();
for (int columnIndex = 0; columnIndex < dt.Columns.Count; columnIndex++)
{
// In case of 2 rows:

// row 1: (0 * 4) + 0 = 0
// row 1: (0 * 4) + 1 = 1
// row 1: (0 * 4) + 2 = 2
// row 1: (0 * 4) + 3 = 3

// row 2: (1 * 4) + 0 = 4
// row 2: (1 * 4) + 1 = 5
// row 2: (1 * 4) + 2 = 6
// row 2: (1 * 4) + 3 = 7

row[columnIndex] = bill[(rowIndex * dt.Columns.Count) + columnIndex];
}

dt.Rows.Add(row);
}


if (dt != null && dt.Rows.Count > 0)
{
totalRec = dt.Rows.Count;
string ReqEnvPath = System.Configuration.ConfigurationManager.AppSettings["ReadEnvPath"].ToString();
XElement SoapReqEnv = XElement.Load(ReqEnvPath);

foreach (DataRow dr in dt.Rows)
{

string uniqueID = dr["APPLICATION_NO"].ToString();
string meterNo = dr["METER_SERIAL_NO"].ToString();

string timestamp = DateTime.UtcNow.ToString("o");
StringBuilder sbArg0 = new StringBuilder();
try
{
sbArg0.AppendFormat(@"<?xml version=""1.0"" encoding=""UTF-8"" ?> " + SoapReqEnv.ToString(), uniqueID, startTS, endTS, timestamp, this.HEXURL, this.HEXUID, this.HEXPWD);
Guid currentGuid = Guid.NewGuid();

obj.getResponseAsync(sbArg0.ToString(), currentGuid + "$" + uniqueID);
obj.getResponseCompleted += this.myHandler;
string delayMS = System.Configuration.ConfigurationManager.AppSettings["DelayMS"].ToString();
ushort delay = 1000;
ushort.TryParse(delayMS, out delay);
System.Threading.Thread.Sleep(delay);


}
catch (Exception ex)
{
error += "Error for UniqID:" + uniqueID + "Desc:" + ex.Message + "\n";
}
finally
{
//System.Threading.Thread.CurrentThread.Join();
}
}
}

return error;
}

如何添加它们?

最佳答案

解决方案一

代码示例;这不是最好或最短的实现,但对于新手程序员来说是一个清晰易懂的实现。

string[] arr = { "1", "2", "3", "4", "5", "6", "7" };

int rowCount = arr.Length / dt.Columns.Count;
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
DataRow row = dt.NewRow();
for (int columnIndex = 0; columnIndex < dt.Columns.Count; columnIndex++)
{
// In case of 2 rows:

// row 1: (0 * 4) + 0 = 0
// row 1: (0 * 4) + 1 = 1
// row 1: (0 * 4) + 2 = 2
// row 1: (0 * 4) + 3 = 3

// row 2: (1 * 4) + 0 = 4
// row 2: (1 * 4) + 1 = 5
// row 2: (1 * 4) + 2 = 6
// row 2: (1 * 4) + 3 = 7

row[columnIndex] = arr[(rowIndex * dt.Columns.Count) + columnIndex];
}

dt.Rows.Add(row);
}

方案二

一个更高级edge-case-safe解决方案,它使用扩展方法将原始数组拆分为多个子数组并填充一个数据表。

public static class Extensions
{
public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
{
for (var i = 0; i < (float)array.Length / size; i++)
{
yield return array.Skip(i * size).Take(size);
}
}

public static void FillDataTable<T>(this DataTable dataTable, T[] input)
{
IEnumerable<IEnumerable<T>> rowValues = input.Split(dataTable.Columns.Count);
foreach (IEnumerable<T> rowValue in rowValues)
{
DataRow row = dataTable.NewRow();

T[] cellValues = rowValue.ToArray();
for (int columnIndex = 0; columnIndex < cellValues.Length; columnIndex++)
{
// 'Safe'-check in case the original array didn't contain enough values. The cell value will remain 'null'
if (columnIndex < dataTable.Columns.Count)
{
row[columnIndex] = cellValues[columnIndex];
}
}

dataTable.Rows.Add(row);
}
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };

DataTable dataTable = new DataTable();
dataTable.Columns.Add("v1");
dataTable.Columns.Add("v2");
dataTable.Columns.Add("v3");
dataTable.Columns.Add("v4");

dataTable.FillDataTable(input);
}
}

关于c# - 将数组添加到列数有限的数据表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55404097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com