SELECT DISTINCT in DataView's RowFilter

二次信任 提交于 2019-12-23 12:38:37

问题


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;

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

Unfortunately, I'm getting this error;

Syntax error: Missing operand after 'DISTINCT' operator.

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?


回答1:


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

You'll probably need to perform your subquery (SELECT DISTINCT parentID FROM myOtherTable) separately.

This article describes the problem and a possible solution.




回答2:


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

DataViews have a ToTable method, and several overloads take a boolean to specify whether to return only the distinct rows.

Here is one method: 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] );




回答3:


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



回答4:


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

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



回答5:


Try just leaving out the "DISTINCT". In this case, the results should be the same with or without. Troubleshoot from there.



来源:https://stackoverflow.com/questions/602836/select-distinct-in-dataviews-rowfilter

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