What does the dataRowFilter do? - c#

What does the dataRowFilter do? is it only a condition for the rows? because adding a select in the row filter does not work
The code is:
Dim strExpr = "ClientID>(select count(*) from Client)-10" (to select the last 10 records);
Dim dv = ds.Tables(0).DefaultView;
dv.RowFilter = strExpr;
Dim newDS = New DataSet();
Dim newDT = dv.ToTable();
newDS.Tables.Add(newDT);
But the code does not work , if I put in strExpr="ClientID>3" the code does work so is the RowFilter only a condition? because I cant put select in it.

The DataRowFilter property is used to filter which rows are viewed in the DataView. The string is internally parsed into DataExpressions. You'll find more information about the expression syntax over at MSDN when looking at the Expression property of the DataColumn class. The same syntax applies to the Select function of the DataTable.
In these LINQ times it's quite trivial to query a data-set/table. For instance, if you have a Client table with indices ranging from 1 to 100, then the following code will return clients with indices 31 to 40.
Dim table As DataTable = (
From row As DataRow
In ds.Tables("Client")
Let clientId As Int32 = row.Field(Of Int32)("ClientID")
Where clientId > 30
Order By clientId Ascending
Select row
).Take(10).CopyToDataTable()
PS: The same query can actually be written as a one-liner:
Dim table As DataTable = (From row As DataRow In ds.Tables("Client") Let clientId As Int32 = row.Field(Of Int32)("ClientID") Where clientId > 30 Order By clientId Ascending Select row).Take(10).CopyToDataTable()

Related

How to Get correct DataColumn Properties from DB

I am using ADO.NET. When I want to save data to table in SQL I need to retrieve columns information in this table. By information I mean Column max size (I want to get 10 from nvarchar(10) column) and NULL or NOT NULL.
I am using next code:
var selectFromCmd = SqlCommandFactory.CreateCommand("select top 0 * from [dbo]." + destTableName, SqlConnection, SqlTransaction);
var dataAdapter = new SqlDataAdapter(selectFromCmd);
var destinationTable = new DataTable();
dataAdapter.Fill(destinationTable);
Then I get DataColumn like so:
var column = destinationTable.Columns["MyColumn"]
But AllowDBNull is always true
and MaxLength is always -1 even if this column is string
So, how can I get the correct information about column properties in ADO.NET ?
I would rather use the sys cataloge views for this query. Something like this....
SELECT c.name ColumnName
,t.Name Datatype
,c.max_length MaxLength
,c.is_nullable
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = object_id('Customers') --<-- your table name

Creating a datatable with only the rows that match a specific column name prefix using Linq in c#

For example my datatable is like this
A_1 A_2 A_3 ..... A_15 B_1.....B_10 C_1....C_10
x y z........ K
1 2 3.........4
I am trying to create seperate datatables for A,B and C which selects the rows based on column prefix, Also i just need row values in my new datatable.
var query = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select table.Rows);
If the above is correct, how to proceed to insert the rows(which is in the query) to the new data table ?
You can create a DataView then copy to a DataTable selecting the columns that match your criteria:
string[] cols = (from dc in table.Columns.Cast<DataColumn>()
where dc.ColumnName.Contains(prefix)
select dc.ColumnName)
.ToArray();
DataView view = new DataView(table);
DataTable selected = view.ToTable(false, cols); // false ==> include "duplicate" rows

C#: How can I select specific rows from the ResultsTable returned from a KeywordQuery?

I am trying to perform a two-pass search against a Sharepoint list. In the first pass, I am using a KeywordQuery to search against the index of all the items. In the second pass, I am applying column value filters chosen by a user by building a select statement.
ResultTableCollection rtc = kwqry.Execute();
ResultTable rt = rtc[ResultType.RelevantResults];
dt = new DataTable();
//Load Data Table
dt.Load(rt, LoadOption.OverwriteChanges);
DataRow[] rows = dt.Select("ColumnName1 = 'foo' AND ColumnName2 = 'bar'");
Where the columns could be multi-value lookup columns in a Sharepoint list. The first pass is working properly and returning the right number of matches in a DataTable. However, when I try to apply the Select statement, I get the following error: Cannot perform '=' operation on System.String[] and System.String. Converting the columns to a string instead of a string array results in the same error, as does using the IN operator.
Am I building my select statement incorrectly? How could I run my second pass filter on my DataTable?
Have you tried with LINQ?
DataTable t1 = new DataTable();
var rows = from x in t1.AsEnumerable()
where x.Field<string[]>("column1name").Contains("foo")
select x;
You have to specify the field type in the Where clause...
Hope it helps.
Try using this, it will work :
DataRow[] rows = dt.Select("(ColumnName1 = 'foo' AND ColumnName2 = 'bar')");

How to get distinct values from a datatable in strongly typed dataset

I am using strongly typed dataset and have many tables in that..
The problem is now i want to filter data from
GetData()
function which has a query like
select * from table_name
How can i filter a particular table and distinct values from it. Also if i try to filter it return all column but rest have null values except the one i asked, so i cannot assign it as a datasource to a datagrid or combobox
How can i do this..
How about the Select() method of the DataTable?
DataRow[] filtered = someDataSet.SomeDataTable.Select("Status = 'Active'");
Edit:
Updated code sample following OP's comment
using System.Linq;
...
DataRow[] rows = someDataSet.SomeDataTable.Select("Status = 'Active'");
string[] columnValues = row.Select(x => x["SomeColumnName"].ToString());
Note, the two Select() methods are different. The first is a DataTable method that filters rows. The second is a linq extension method that is transforming an array of rows into an array of strings.
Your question is not very clear. What I have understood is, you have multiple tables coming in a dataset. Now you want to filter based about Table Name. If you are returning multiple tables in dataset by writing multiple select queries in a single stored procedure, then there is no way you could name the tables in sql. You have to access it by hard coded way.
Another way could be you could add a table at 0th position and in that table add the name of the table and position that it would be in DataSet while returning the query. So the first query in your stored procedure would return a table which has mapping between name of the table and postion they are in DataSet. Now GetData() function would become very easy.
function DataTable GetData(string tableName)
{
//Supposing 0th table is mapping table with 2 columns, One contains Name and another position
var pos = ds.Tables[0].where(x => x[0] == tableName).Select(x => x[1]).firstOrDefault();
var table = ds.Tables[pos];
return table;
}
As I understood your question, I did something quick to try to help, the code can be improved and must be, how I said I did it fast.
Public Module DataSetExtensions
<Runtime.CompilerServices.Extension()>
Public Function [Select](ds As DataSet, table As String, ParamArray campos() As String) As DataTable
Dim dt As New DataTable
Dim sourceTable = (From t As DataTable In ds.Tables _
Where t.TableName = table).SingleOrDefault
Dim columnas = From c As DataColumn In sourceTable.Columns Where campos.Contains(c.ColumnName)
columnas.ToList.ForEach(Sub(c) dt.Columns.Add(c.ColumnName))
For Each row As DataRow In sourceTable.Rows
Dim newRow As DataRow = dt.NewRow
For Each col As DataColumn In sourceTable.Columns
If columnas.Contains(col) Then
newRow(col.ColumnName) = row(col)
End If
Next
dt.Rows.Add(newRow)
Next
Return dt
End Function
<Runtime.CompilerServices.Extension()>
Public Function [Select](table As DataTable, ParamArray campos() As String) As DataTable
Dim dt As New DataTable
Dim columnas = From c As DataColumn In table.Columns Where campos.Contains(c.ColumnName)
columnas.ToList.ForEach(Sub(c) dt.Columns.Add(c.ColumnName))
For Each row As DataRow In table.Rows
Dim newRow As DataRow = dt.NewRow
For Each col As DataColumn In table.Columns
If columnas.Contains(col) Then
newRow(col.ColumnName) = row(col)
End If
Next
dt.Rows.Add(newRow)
Next
Return dt
End Function
End Module
And the call something like this
Using ds As New DataSet1()
Using ta As New DataSet1TableAdapters.BCR_SOLICITUDTableAdapter()
ta.Fill(ds.BCR_SOLICITUD)
Dim dt As DataTable
' First extended method
dt = ds.Select("BCR_SOLICITUD", "Numero", "Estado", "DescripciĆ³n")
' Second extended method
dt = ds.BCR_SOLICITUD.Select("Numero","Estado", "DescripciĆ³n")
'Code here
dt.Dispose
dt=Nothing
End Using
End Using
You could use using in DataTable, but this is not the topic.
I hope it help you.

SELECT DISTINCT in DataView's RowFilter

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?
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.
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] );
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";
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
Try just leaving out the "DISTINCT". In this case, the results should be the same with or without. Troubleshoot from there.

Categories