本文整理匯總了C#中System.Data.DataTable.ToArray方法的典型用法代碼示例。如果您正苦於以下問題:C# DataTable.ToArray方法的具體用法?C# DataTable.ToArray怎麽用?C# DataTable.ToArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.ToArray方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ToJson
public static string ToJson(DataTable dt)
{
//array
if (dt.Columns.Count == 1)
{
//string name = dt.Columns[0].ColumnName;
string json = VAL.Boxing(dt.ToArray(row => row[0])).ToJson();
//string.Format("{0}={1}", name, json);
return json;
}
string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray();
VAL L = new VAL();
foreach (DataRow row in dt.Rows)
{
VAL V = new VAL();
for (int i = 0; i < columns.Length; i++)
{
V.AddMember(columns[i], row[i]);
}
L.Add(V);
}
return L.ToJson();
}
示例2: FromTableToArrayTest
public void FromTableToArrayTest()
{
DataTable table = new DataTable();
table.Columns.Add("A", typeof(bool));
table.Columns.Add("B", typeof(string));
table.Rows.Add(true, "1.0");
table.Rows.Add(true, "0");
table.Rows.Add(false, "1");
table.Rows.Add(false, "0.0");
double[][] actual = table.ToArray(System.Globalization.CultureInfo.InvariantCulture);
double[][] expected =
{
new double[] { 1, 1 },
new double[] { 1, 0 },
new double[] { 0, 1 },
new double[] { 0, 0 },
};
Assert.IsTrue(expected.IsEqual(actual));
}
示例3: ToArrayTest1
public void ToArrayTest1()
{
DataTable table = new DataTable("myData");
table.Columns.Add("Double", typeof(double));
table.Columns.Add("Integer", typeof(int));
table.Columns.Add("Boolean", typeof(bool));
table.Rows.Add(4.20, 42, true);
table.Rows.Add(-3.14, -17, false);
table.Rows.Add(21.00, 0, false);
double[][] expected =
{
new double[] { 4.20, 42, 1 },
new double[] { -3.14, -17, 0 },
new double[] { 21.00, 0, 0 },
};
double[][] actual = table.ToArray();
Assert.IsTrue(expected.IsEqual(actual));
string[] expectedNames = { "Double", "Integer", "Boolean" };
string[] actualNames;
table.ToArray(out actualNames);
Assert.IsTrue(expectedNames.IsEqual(actualNames));
}
示例4: FindAll
public List<ScheduledEvents> FindAll()
{
DataTable dt = new DataTable();
dt.Columns.Add("key", typeof(string));
dt.Columns.Add("scheduletype", typeof(string));
dt.Columns.Add("exetime", typeof(string));
dt.Columns.Add("lastexecuted", typeof(DateTime));
dt.Columns.Add("issystemevent", typeof(bool));
dt.Columns.Add("enable", typeof(bool));
EventInfo[] events = ScheduleConfigs.GetConfig().Events;
foreach (EventInfo ev in events)
{
DataRow dr = dt.NewRow();
dr["key"] = ev.Key;
dr["scheduletype"] = ev.ScheduleType;
if (ev.TimeOfDay != -1)
{
dr["exetime"] = "定時執行:" + (ev.TimeOfDay / 60) + "時" + (ev.TimeOfDay % 60) + "分";
}
else
{
dr["exetime"] = "周期執行:" + ev.Minutes + "分鍾";
}
DateTime lastExecute = Event.GetLastExecuteScheduledEventDateTime(ev.Key, Environment.MachineName);
if (lastExecute == DateTime.MinValue)
{
dr["lastexecuted"] = Convert.ToDateTime("1999-01-01").ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
dr["lastexecuted"] = lastExecute.ToString("yyyy-MM-dd HH:mm:ss");
}
dr["issystemevent"] = ev.IsSystemEvent.ToString();// ? "係統級" : "非係統級";
dr["enable"] = ev.Enabled.ToString();// ? "啟用" : "禁用";
dt.Rows.Add(dr);
}
var list = dt.ToArray<ScheduledEvents>();
return list.ToList();
}
示例5: ToCSharp
private static string ToCSharp(DataTable dt, string clss)
{
clss = clss ?? string.Empty;
//array
if (dt.Columns.Count == 1)
{
var L1 = dt.ToArray(row => VAL.Boxing(row[0]).ToString());
return "{" + string.Join(",", L1) + "}";
}
string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray();
List<string> L = new List<string>();
foreach (DataRow row in dt.Rows)
{
List<string> V = new List<string>();
for (int i = 0; i < columns.Length; i++)
{
V.Add(string.Format("{0}={1}", columns[i], VAL.Boxing(row[i]).ToString()));
}
L.Add($"new {clss}" + "{" + string.Join(", ", V) + "}");
}
return $"new {clss}[]" + "{\n" + string.Join(",\n", L) + "\n}";
}