I want to add table name as string and table value as arraylist if table name exist in same key add record as array list ...here showing key already exist . where i can modify :
SortedDictionary<string, ArrayList> DealInfo = new SortedDictionary<string, ArrayList>();
ArrayList Deal = new ArrayList();
string DealName=string.Empty;
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow dr in table.Rows)
{
if (!DealInfo.ContainsKey(Convert.ToString(dr["DealTab"])))
{
DealName = Convert.ToString(dr["DealTab"]);
}
Deal.Add(dr);
DealInfo.Add(DealName, Deal);
}
}
It's hard to tell exactly what you want since your description says you want to use the TableName as the key, but your code is adding a column from the row as a key.
Going with the idea that the TableName should be the key, something like this should work:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
if (!dealInfo.ContainsKey(table.TableName))
{
dealInfo.Add(table.TableName, table.Rows.Cast<DataRow>().ToList());
}
}
Going with the idea that you want to key off a column in each row for the key, you could do something like:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow row in table.Rows)
{
var dealName = row["DealTab"].ToString();
if (dealInfo.ContainsKey(dealName))
{
dealInfo[dealName].Add(row);
}
else
{
dealInfo.Add(dealName, new List<DataRow> {row});
}
}
}
To fill a ListView with data from a row for a specific DealName, you can find the dictionary entry for that deal name and access the list of rows this way:
foreach (DataRow row in dealInfo["SomeDealName"])
{
// Here you have access to the rows where row["DealTab"] == "SomeDealName"
// You can fill a list view with some column value from the row like:
listView1.Add(row["SomeColumnName"].ToString());
}
Related
I have a list of 2 DataTables. I want to iterate through each of them, one at a time. How do I do this? There are 0 examples of going through a list of DataTables.
List<DataTable> test = new List<DataTable>();
foreach (DataRow dataRow in TEST.LIST)
{
string value = dataRow.Field<string>("Slave_IO_Running"); //Looks for "Slave_IO_Running" status.
if (value == "Yes")
{
results.Add(siteName + ": WORKING"); //adds working to the visual table
}
else
{
results.Add(siteName + ": REPLICATION ERROR"); //adds not working to the result list
}
break;
}
for each datatable you can use DataTable.Rows and for each row you can access the properties as row["columnName"] or traverse each column in the corresponding row
like
foreach(DataTable table in tables)
{
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
}
You can try it like this, assuming both tables contain the same columns:
foreach (DataRow dataRow in test.SelectMany(dt => dt.Rows.OfType<DataRow>()))
{
// your code using the rows
}
I want to remove equal entries from DataTable.
I tried DefaultView, but it only removes the equals and not all entries which including them.
DataView view = table1.DefaultView;
DataTable tbl = view.ToTable();
return tbl;
you can do this
public DataTable RemoveDuplicate(DataTable dataTable, string columname)
{
Hashtable hashTable = new Hashtable();
List<String> duplicates = new List<String>();
foreach (DataRow datarow in dataTable.Rows)
{
if (hashTable .Contains(datarow [columname]))
{
duplicateList.Add(datarow );
}
else
{
hashTable .Add(datarow [columname], string.Empty);
}
}
//Now remove the duplicates .
foreach (DataRow datarow in duplicates )
dataTable.Rows.Remove(datarow );
return dataTable;
}
The code below choose the first row and remove the other row below with specific id. The things that i want is the last line within the id.
var dtremove = RemoveDuplicateRows(dt, "id);
This the extension
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dt.Rows)
{
if (hTable.Contains(drow[colName]))
{
duplicateList.Add(drow);
}
else
hTable.Add(drow[colName], string.Empty);
}
//duplicateList.Sort();
//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)
dt.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output.
return dt;
}
The example of datatable.
id date
------------
A 1/1/2018
A 1/2/2018
A 1/3/2018
B 2/1/2018
B 2/2/2018
i want the result like this.
id date
------------
A 1/3/2018
B 2/2/2018
I tried not to change too much of your code, pretty sure it can be improved, but the changes below looks for the value and if it already exists in the dictionary, it replaces it: Thus, you will end up with the very last row instead of the first one. Also, it returns a new DataTable and the original one will stay intact.
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
var uniqueRows = new Dictionary<string, DataRow>();
foreach (DataRow thisRrow in dt.Rows)
{
if (uniqueRows.ContainsKey(colName))
{
uniqueRows[colName] = thisRrow;
}
else
{
uniqueRows.Add(colName, thisRrow);
}
}
DataTable copy = dt.Copy();
copy.Rows.Clear();
foreach (var thisRow in uniqueRows)
{
copy.Rows.Add(thisRow.Value);
}
//Datatable which contains unique records will be return as output.
return copy;
}
Dataset consists of a table of two columns. Table name=Project. One column name=Name, other column name=Resource. There are multiple repetitive project names.
var dataset = FakeDataset.CreateDataset();
var projectList = new List<Project>();
foreach (DataTable table in dataset.Tables)
{
foreach (DataRow dataRow in table.Rows)
{
projectList.Add(new Project { Name = Convert.ToString(dataRow["Name"]), Resource = Convert.ToString(dataRow["Resource"]) });
}
}
Now I am creating a Project object every time for a single project Name in the dataset. What I want is - to create Project object only for unique project Name in dataset. I am a beginner so would be nice a simple solution.
Not tested and I'm not sure if I understood you correctly, but from my assumptions
you want something like this:
var dataset = FakeDataset.CreateDataset();
var projectList = new List<Project>();
foreach (DataTable table in dataset.Tables)
{
foreach (DataRow dataRow in table.Rows)
{
if(projectList.Any(Project => Project.Name == dataRow["Name"]))
projectList.Add(new Project { Name = Convert.ToString(dataRow["Name"]), Resource = Convert.ToString(dataRow["Resource"]) });
}
}
Edit:
var dataset = FakeDataset.CreateDataset();
var projectList = new List<Project>();
foreach (DataTable table in dataset.Tables)
{
foreach (DataRow dataRow in table.Rows)
{
if (!projectList.Any(Project => Project.Name == Convert.ToString(dataRow["Name"])))
{
projectList.Add(new Project { Name = Convert.ToString(dataRow["Name"]), Resource = Convert.ToString(dataRow["Resource"]) });
}
else
{
Project p = projectList.Find(Project => Project.Name == Convert.ToString(dataRow["Name"]));
projectList[projectList.IndexOf(p)].Resource += "\r\n" + Convert.ToString(dataRow["Name"]);
}
}
}
I have a DataSet named DataSet1. It contains an unknown number of tables and an unknown number of columns and rows in those tables. I would like to loop through each table and look at all of the data in each row for each column. I'm not sure how to code this. Any help would be appreciated!
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
// read item
}
}
}
Or, if you need the column info:
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
object item = row[column];
// read column and item
}
}
}
Just loop...
foreach(var table in DataSet1.Tables) {
foreach(var col in table.Columns) {
...
}
foreach(var row in table.Rows) {
object[] values = row.ItemArray;
...
}
}