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..
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;
}
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.