Create ADO.NET DataView showing only selected Columns

那年仲夏 提交于 2019-12-18 21:52:08

问题


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 (π)?


回答1:


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("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");



回答2:


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...




回答3:


create dataview as a swap from one table to other table, and use the dtswap as datasource.

DataView dw = new DataView(dtfee);
            DataTable dtswap = new DataTable();
            dtswap = dw.ToTable(true,"Fees", "FeeAmount", "Year", "CollectorName", "Month");



回答4:


DataSet and its associated types have no ability to perform relational operations.



来源:https://stackoverflow.com/questions/1201255/create-ado-net-dataview-showing-only-selected-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!