C# 在 DataView 的 RowFilter 中选择 DISTINCT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602836/
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
SELECT DISTINCT in DataView's RowFilter
提问by Christopher McAtackney
I'm trying to narrow down the rows that are in my DataView based on a relation with another table, and the RowFilter I'm using is as follows;
我试图根据与另一个表的关系缩小我的 DataView 中的行,我使用的 RowFilter 如下;
dv = new DataView(myDS.myTable,
"id IN (SELECT DISTINCT parentID FROM myOtherTable)",
"name asc",
DataViewRowState.CurrentRows);
"myTable" and "myOther" table are related via myTable.ID and myOtherTable.parentID, and so the idea is that the DataView should only contain rows from "myTable" which have corresponding child rows in "myOtherTable".
"myTable" 和 "myOther" 表通过 myTable.ID 和 myOtherTable.parentID 相关,所以这个想法是 DataView 应该只包含来自 "myTable" 的行,这些行在 "myOtherTable" 中有相应的子行。
Unfortunately, I'm getting this error;
不幸的是,我收到了这个错误;
Syntax error: Missing operand after 'DISTINCT' operator.
语法错误:'DISTINCT' 运算符后缺少操作数。
The SQL is fine as far as I am aware, so I'm wondering is there some limitation on using the DISTINCT keyword as part of RowFilter's SQL? Anyone have any idea?
据我所知,SQL 很好,所以我想知道将 DISTINCT 关键字用作 RowFilter 的 SQL 的一部分是否有一些限制?任何人有任何想法?
采纳答案by Matt Peterson
Unfortunately, I don't think you can perform a subquery in a DataView's filter expression. You're only allowed to use a subset of SQL in some expressions (documented here).
不幸的是,我认为您不能在 DataView 的过滤器表达式中执行子查询。您只能在某些表达式中使用 SQL 的一个子集(在此处记录)。
You'll probably need to perform your subquery (SELECT DISTINCT parentID FROM myOtherTable
) separately.
您可能需要SELECT DISTINCT parentID FROM myOtherTable
单独执行子查询 ( )。
This articledescribes the problem and a possible solution.
本文描述了该问题和可能的解决方案。
回答by Mark A Johnson
Try just leaving out the "DISTINCT". In this case, the results should be the same with or without. Troubleshoot from there.
尝试只省略“DISTINCT”。在这种情况下,无论有无,结果都应该相同。从那里进行故障排除。
回答by DarrellNorton
Unfortunately you can't do it that way, as the RowFilter property does not support the distinct keyword. Here is the list of expressions you can perform in a RowFilter (which is just a DataColumn Expression): http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
不幸的是,您不能这样做,因为 RowFilter 属性不支持 distinct 关键字。以下是您可以在 RowFilter(它只是一个 DataColumn 表达式)中执行的表达式列表:http: //msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
DataViews have a ToTable method, and several overloads take a boolean to specify whether to return only the distinct rows.
DataViews 有一个 ToTable 方法,几个重载使用一个布尔值来指定是否只返回不同的行。
Here is one method: http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx
这是一种方法:http: //msdn.microsoft.com/en-us/library/wec2b2e6.aspx
Here is how you would use it:
以下是您将如何使用它:
DataTable newDataTable = myDataView.ToTable( true, [array of column names as strings] );
DataTable newDataTable = myDataView.ToTable( true, [列名数组作为字符串] );
回答by Arshed Mahmood
the following code is extracting distinct values/records from a table/dataview, namely(PROD_DESP_TRN) having field(CONTAINER_NO) Finally, this code is filling a combobox(cmbContainerNo) with unique values/records
以下代码从表/数据视图中提取不同的值/记录,即(PROD_DESP_TRN) 具有字段(CONTAINER_NO) 最后,此代码正在填充具有唯一值/记录的组合框(cmbContainerNo)
Form Level Declaration:
表单级别声明:
Dim dsLocal As DataSet
Dim dvm As DataViewManager
Private Sub FillcomboContainer()
Try
Dim dv As DataView = New DataView
cmbContainerNo.DataSource = Nothing
dv = dvm.CreateDataView(dsLocal.Tables("PROD_DESP_TRN"))
dv.Sort = "CONTAINER_NO"
cmbContainerNo.DataSource = dv.ToTable(True, "CONTAINER_NO")
cmbContainerNo.DisplayMember = "CONTAINER_NO"
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
End Sub
回答by Arunv.innovaminds
DataView dvBindAssignedByDropDown = new DataView();
DataTable dtBindAssignedByDropDown = new DataTable();
dvBindAssignedByDropDown = ds.Tables[0].DefaultView;
string[] strColnames=new string[2];
strColnames[0] = "RedNames";
strColnames[1] = "RedValues";
dtBindAssignedByDropDown = dvBindAssignedByDropDown.ToTable(true, strColnames);
ddlAssignedby.DataTextField = "RedNamesNames";
ddlAssignedby.DataValueField = "RedNames";
ddlAssignedby.DataSource = dtBindAssignedByDropDown;
ddlAssignedby.DataBind();
ddlAssignedby.Items.Insert(0, "Assigned By");
ddlAssignedby.Items[0].Value = "0";