how to find rows which have specific column value in datatable? - c#

I am getting data from sql in datatable which have a column names userid,username and department.I want to get the rows in another datatable by the specific department.
datatable
I have attached datatable.I just want a datatable which contain the rows which have department "Engineering" from the first datatble
thanks in advance

You want to use the DataTable Select method which will return an array of DataRow objects.
DataRow[] result = myTable.Select("Department = 'Engineering'");
Just replace 'myTable' with your DataTable.
Here is some reference info for the Select method.
http://www.dotnetperls.com/datatable-select
https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx

Select userid,username,department where department ='Engineering';
Do you meant the above?

Related

LINQ to remove rows from datatable whose values in list<string>

I have datatable with two columns: id, title.
And i need to remove from the table the rows whose 'title' column values are included in the list.
Think the most correct way to do this is with linq to datatable, but dont know how.
Help me please build linq query.
Or can there be another effective way? More than 100k rows in this table.
You can use:
List<DataRow> rowsToRemove = table.AsEnumerable()
.Where(r => list.Contains(r.Field<string>("title")))
.ToList();
rowsToRemove.ForEach(table.Rows.Remove);

DataTable Sorting Then Assign Sorted Table Back To Original Table

I have a dataset that has a table called detail I want to sort. I know using a dataview I can sort the table with the following code..
Dim dvParsedDataset As New DataView(parsedDataset.Tables("Detail"))
dvParsedDataset.Sort = AuthorizatonConstants.Auth_ID
I want to not only sort the table but assign it back to the original table in this case parsedDataset.Tables("Detail") but when assigning the sorted view back I get a read only error
parsedDataset.Tables("Detail") = dvParsedDataset.ToTable 'READ ONLY ERROR
How do I sort the table and also override the original table with the sorted table?
You could remove the previous DataTable from the DataSet and the add the new table
DataTable detailCopy = dvParsedDataset.ToTable("Detail")
parsedDataset.Tables.Remove("Detail")
parsedDataset.Tables.Add(detailCopy)

How to loop data from a table in sql server and using the returned data to query another table. Please see

This is what I am trying to achieve :
I have a table in a database in which there is a column named "item_code".
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
I tried using a datareader object in a while loop, fetch one row at a time and then query the other table inside this loop to fetch the rows required but I couldn't figure out how to put this data in a gridview (use datatable? if yes, how?) in such a way that the previous rows in the gridview don't get erased after each iteration of the while loop.
The only way I know for putting data into a gridview is by using .Fill() but obviously, Fill method wouldn't do in this case as it would wipe out the previous entries in the gridview.
Please help.
Your solution will work, but you are correct, Fill() will erase the contents of the table. Instead, use Merge()
var myMainTable = new DataTable();
foreach(var itemId in itemIds)
{
var currentTable = new DataTable();
// submit new query
myAdapter.Fill(currentTable)
myMainTable.Merge(currentTable);
}
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
You could use a single SQL query that joins the two tables on "item_code" and retrieves the results from the second table.
You can make it in SQL level in one step, like this:
SELECT Table2.*
FROM Table2
INNER JOIN Table1 ON Table1.item_code = Table2.item_code
ORDER BY Table2.item_code
And of course if you need to make a smaller list, you can wirte the WHERE too.

Remove duplicated from ONLY FIRST column in DataTable in C#

I have a DataTable containing 10 rows and 2 columns which is created from a mdx query, I want to convert this DataTable into html table but before that I want remove duplicated from ONLY FIRST column. How can I do that?
Use LINQ and query it yourself:
myTable.Tables[0].Rows.GroupBy(x => x["Column1Name"]);
Your rows should now be grouped by a distinct Column1Name value, and you can then access it from there.

How to make relation between tables which are in same dataset?

I have one dataset in which there are
40 tables. Now I want to make a relation
between these tables and show
important data in grid. How do i do this?
If you're creating a typed dataset, it's easiest to create the relations in Visual Studio's dataset designer. Just right-click on the table in the designer, select Add->Relation, and specify the relation.
If you need to specify the relation in code, you can do it like this:
dataSet.Relations.Add(dataSet.Tables["Customers"].Columns["customerId"],
dataSet.Tables["Orders"].Columns["customerId"]);
Read all about it in MSDN here.
That is a large number of DataTables to have in a DataSet.
The first thing I would consider would be to reduce the number of DataTables (and eliminate the need for Relations) by populating the DataTables with queries that JOIN the database tables. For example, instead of having one DataTable for Product Category and another for Product Detail, it might be possible to combine the data for both database tables into one DataTable. Similarly, for Customer, Customer Address and Customer Phone, retrieve all of the data in one DataTable by using one query that does a JOIN on all three database tables.
Once you have minimized the number of DataTables in the DataSet, you can add Relations between DataTables if they have matching columns (even if the columns have different names). For example, there might be an Orders DataTable with a CustomerID column that matches the ID column in the Customers DataTable.
Here is the code to add a Relation to the DataSet for that situation. Assume that we have a DataSet dst containing two DataTables Customers and Orders.
DataColumn customerColumn, orderColumn;
customerColumn = dst.Tables["Customers"].Columns["ID"];
orderColumn = dst.Tables["Orders"].Columns["CustomerID"];
DataRelation dr = new DataRelation("CustomerOrders", customerColumn, orderColumn);
dst.Relations.Add(dr);
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
Did you try something like:
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
private void CreateRelation()
{
// Get the DataColumn objects from two DataTable objects
// in a DataSet. Code to get the DataSet not shown here.
DataColumn parentColumn =
DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn childColumn =
DataSet1.Tables["Orders"].Columns["CustID"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders",
parentColumn, childColumn);
// Add the relation to the DataSet.
DataSet1.Relations.Add(relCustOrder);
}

Categories

Resources