Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ADO.NET DataView showing only selected Columns

In C# & .NET, can one create a DataView that includes only a proper subset of the DataColumns of a given DataTable?

In terms of relational algebra, one assigns a RowFilter in order to perform a "selection" operation (σ). How would one perform a "projection" operation (π)?

like image 649
JaysonFix Avatar asked Jul 29 '09 15:07

JaysonFix


2 Answers

You can't do that, but you can create a copy of the table with only the columns you want :

DataView view = new DataView(table);
DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn", "ThirdColumn");

Optionally you can return rows that have distinct values for the selected columns :

DataView view = new DataView(table);
DataTable table2 = view.ToTable(true, "FirstColumn", "SecondColumn", "ThirdColumn");
like image 164
Thomas Levesque Avatar answered Oct 04 '22 03:10

Thomas Levesque


Well I can't see any reason for "wanting" to do that... Remember, a DataView is just a list of pointers to the rows in the original table, and there is obviously no way to remove columns from the the original table... at least not without affecting every other function utilizing that table... Just only use the columns you want...

like image 35
Charles Bretana Avatar answered Oct 04 '22 02:10

Charles Bretana