I need to solve a very very simple filter problem, but I'm new using DataTables, DataRows and so on. The solution I propo开发者_如何学运维sed smells a lot. I'm pretty sure that is a better way to implement it.
Here is the simple problem explained:
I have two dataTables called branches and changesets. The dataTable called branches has all the entries in the system, and I need to filter those branches that are not referenced in the changeset entries. A changeset references a branch in a field called FIDBRANCH.
For example. Suppose a dateTable with 6 branches and 3 changesets. Only branches with ids 4 and 5 are referenced.
BRANCHES
1
2
3
4
5
6
CHANGESETS
references to branch 4
references to branch 5
references to branch 5
The result would be the branches dataset filtered only having branches 4 and 5.
This is my bad approach:
private void FilterUnusedBranches(DataTable branches, DataTable changesets)
{
// index referenced branch ids
ArrayList branchIds = new ArrayList();
foreach (DataRow row in changesets.Rows)
{
long id = Convert.ToInt64(row["FIDBRANCH"]);
branchIds.Add(id);
}
IList<DataRow> notNeededRows = new List<DataRow>();
// get a list of non-referenced rows
foreach (DataRow row in branches.Rows)
{
long id = Convert.ToInt64(row["BRID"]);
if (!branchIds.Contains(id))
{
notNeededRows.Add(row);
}
}
// remove not needed rows
foreach (DataRow row in notNeededRows)
{
branches.Rows.Remove(row);
}
}
You my wish to create a DataRelation between the tow data tables.
This may help you... http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/f122d7f4-3b7d-4d93-bd0f-8bb57cd990a4
精彩评论