L'affectation d'une collection de rangée (obtenue par une requête linq) à une table de données sans utiliser une boucle?

StackOverflow https://stackoverflow.com/questions/6342291

  •  27-10-2019
  •  | 
  •  

Question

I scénario où AVONS datatable peut contenir un grand nombre de lignes. Par conséquent je ne peux pas itérer et mettre à jour le datatable en utilisant une boucle.

Je suis en utilisant le code suivant pour obtenir la collecte de ligne,

         from row in CSVDataTable.AsEnumerable()
         where CSVDataTable.Columns.Cast<DataColumn>().Any(col => !row.IsNull(col))
         select row;

Toute personne s'il vous plaît me dire comment affecter le résultat du code ci-dessus à un datatable sans utiliser une boucle.

Était-ce utile?

La solution

I could assign the Linq query result to a data table by the following code,

           // Create a DataTable from Linq query.       

             IEnumerable<DataRow> query = from row in CSVDataTable.AsEnumerable()
                                            where CSVDataTable.Columns.Cast<DataColumn>().Any(col => !row.IsNull(col))
                                            select row; //returns IEnumerable<DataRow>

             DataTable CSVDataTableWithoutEmptyRow = query.CopyToDataTable<DataRow>();

See the link for further details,

http://msdn.microsoft.com/en-us/library/bb386921.aspx

Autres conseils

You are trying to avoid the unavoidable I think.

You have a "lazy" query wich returns an IEnumerable<DataRow>. This query will be enumerated no matter what when you try to access the DataRow collection it represents.

The only difference will be if you do it directly or some method of DataTable hides that implementation detail.

I'd do the following:

DataTable table;
table.BeginLoadData();
foreach (DataRow row in query)
{
     table.ImportRow(row);
}
table.EndLoadData();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top