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.
Related
I have 2 .Net DataTables. The tables both have the same column definitions.
How do I remove all the rows in Table1 that exist in Table2?
I know I can do this using nested foreach() loops and comparing the ItemArrays.
How would I do this using either Linq or a Lambda expression?
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);
I have a source excel file where my "key" is held in column B. I want to add rows that contain different distinct key values to different tables in a database, using a list of datatables to allow for the number of distinct keys to be variable.
Is it better practise to select the distinct records straight from the sheet itself, or to pull the entire sheet into a "master" datatable and query the datatable for my separate values?
It would be better if you first use distinct and then filter records, it will improve speed and performance.
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?
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.