mercredi 16 avril 2014

c# - copie uniquement les données sélectionnées à nouveau datatable avec linq - Stack Overflow


I've searched the web for quite some time now and can't seem to find an elegant way to



  • read data from one datatable,

  • group it by two variables with linq

  • select only those two variables (forget about the others in the source datatable) and

  • copy these items to a new datatable.


I got it working without selecting specific variables, but at the amount of data the program is going to process later I'd rather only copy what's really needed.


var temp123 = from row in oldDataTable.AsEnumerable()
orderby row["Column1"] ascending
group row by new { Column1 = row["Column1"], Column2 = row["Column2"] } into grp
select grp.First();

newDataTable = temp123.CopyToDataTable();

Can anyone please be so kind to help me out here? Thanks!




You can use custom implementation of CopyToDataTable method from this article How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow


newDataTable = 
oldDataTable
.AsEnumerable()
.GroupBy(r => new { Column1 = row["Column1"], Column2 = row["Column2"] })
.Select(g => g.First())
.OrderBy(x => x.Column1)
.CopyToDataTable(); // your custom extension

Another option, as Tim suggested - manual creation of DataTable.


var newDataTable = new DataTable();
newDataTable.Columns.Add("Column1");
newDataTable.Columns.Add("Column2");

foreach(var item in temp123)
newDataTable.Rows.Add(item.Column1, item.Column2);

And last option (if possible) - don't use DataTable - simply use collection of strongly typed objects.



I've searched the web for quite some time now and can't seem to find an elegant way to



  • read data from one datatable,

  • group it by two variables with linq

  • select only those two variables (forget about the others in the source datatable) and

  • copy these items to a new datatable.


I got it working without selecting specific variables, but at the amount of data the program is going to process later I'd rather only copy what's really needed.


var temp123 = from row in oldDataTable.AsEnumerable()
orderby row["Column1"] ascending
group row by new { Column1 = row["Column1"], Column2 = row["Column2"] } into grp
select grp.First();

newDataTable = temp123.CopyToDataTable();

Can anyone please be so kind to help me out here? Thanks!



You can use custom implementation of CopyToDataTable method from this article How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow


newDataTable = 
oldDataTable
.AsEnumerable()
.GroupBy(r => new { Column1 = row["Column1"], Column2 = row["Column2"] })
.Select(g => g.First())
.OrderBy(x => x.Column1)
.CopyToDataTable(); // your custom extension

Another option, as Tim suggested - manual creation of DataTable.


var newDataTable = new DataTable();
newDataTable.Columns.Add("Column1");
newDataTable.Columns.Add("Column2");

foreach(var item in temp123)
newDataTable.Rows.Add(item.Column1, item.Column2);

And last option (if possible) - don't use DataTable - simply use collection of strongly typed objects.


0 commentaires:

Enregistrer un commentaire