Modo semplice per convertire datarow array datatable
Domanda
Voglio convertire un allineamento DataRow
in DataTable
... Qual è il modo più semplice per fare questo?
Soluzione
Perché non scorrere l'array DataRow e aggiungere (usando DataRow.ImportRow, se necessario, per ottenere una copia del DataRow), qualcosa come:
foreach (DataRow row in rowArray) {
dataTable.ImportRow(row);
}
Assicurati che il tuo dataTable ha lo stesso schema come le DataRow nella propria matrice DataRow.
Altri suggerimenti
Per .Net Framework 3.5 +
DataTable dt = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt1 = dr.CopyToDataTable();
Ma se c'è nessuna riga nella matrice, può causare gli errori come La fonte non contiene DataRows . Pertanto, se si decide di utilizzare questo metodo CopyToDataTable()
, si dovrebbe verificare la matrice di sapere che ha DataRow o meno.
if (dr.Length > 0)
DataTable dt1 = dr.CopyToDataTable();
di riferimento disponibile al MSDN: DataTableExtensions.CopyToDataTable Method (IEnumerable)
DataTable dt = new DataTable();
DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");
dt.Rows.Add(dr);
Un altro modo è quello di utilizzare un DataView
// Create a DataTable
DataTable table = new DataTable()
...
// Filter and Sort expressions
string expression = "[Birth Year] >= 1983";
string sortOrder = "[Birth Year] ASC";
// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);
// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");
modo semplice è:
// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();
// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());
DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{
dt.ImportRow(dr);
}
DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();
DataTable Assetdaterow =
(
from s in dtResourceTable.AsEnumerable()
where s.Field<DateTime>("Date") == Convert.ToDateTime(AssetDate)
select s
).CopyToDataTable();
Ecco la soluzione. Dovrebbe funzionare bene.
DataTable dt = new DataTable();
dt = dsData.Tables[0].Clone();
DataRows[] drResults = dsData.Tables[0].Select("ColName = 'criteria');
foreach(DataRow dr in drResults)
{
object[] row = dr.ItemArray;
dt.Rows.Add(row);
}
Incase chiunque ne ha bisogno in VB.NET:
Dim dataRow as DataRow
Dim yourNewDataTable as new datatable
For Each dataRow In yourArray
yourNewDataTable.ImportRow(dataRow)
Next
Net 3.5+ DataTableExtensions aggiunti, utilizzare il metodo DataTableExtensions.CopyToDataTable
Per datarow gamma basta usare .CopyToDataTable () e tornerà DataTable.
Per uso singolo datarow
new DataRow[] { myDataRow }.CopyToDataTable()
Si potrebbe usare System.Linq in questo modo:
if (dataRows != null && dataRows.Length > 0)
{
dataTable = dataRows.AsEnumerable().CopyToDataTable();
}
DataTable dataTable = new DataTable();
dataTable = OldDataTable.Tables[0].Clone();
foreach(DataRow dr in RowData.Tables[0].Rows)
{
DataRow AddNewRow = dataTable.AddNewRow();
AddNewRow.ItemArray = dr.ItemArray;
dataTable.Rows.Add(AddNewRow);
}
È necessario clonare la struttura della tabella di dati prima quindi importare righe utilizzando ciclo for
DataTable dataTable =dtExisting.Clone();
foreach (DataRow row in rowArray) {
dataTable.ImportRow(row);
}