C# 创建仅显示选定列的 ADO.NET DataView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1201255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:45:04  来源:igfitidea点击:

Create ADO.NET DataView showing only selected Columns

c#.netdatatabledataviewdatacolumn

提问by JaysonFix

In C# & .NET, can one create a DataViewthat includes only a propersubset of the DataColumns of a given DataTable?

在 C# 和 .NET 中,可以创建一个DataView只包含给定 s 的适当子集的DataColumnsDataTable吗?

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

在关系代数方面,分配 aRowFilter以执行“选择”操作(σ)。如何执行“投影”操作(π)?

采纳答案by Thomas Levesque

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

回答by Charles Bretana

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

好吧,我看不出有任何理由“想要”这样做……请记住,DataView 只是指向原始表中行的指针列表,显然无法从原始表中删除列...至少不会影响使用该表的所有其他功能......只使用你想要的列......

回答by John Saunders

DataSetand its associated types have no ability to perform relational operations.

DataSet及其关联类型无法执行关系操作。

回答by aamir

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

创建数据视图作为从一个表到另一个表的交换,并使用 dtswap 作为数据源。

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