I'm working on a system that currently has a fairly complicated function that returns a DataTable, which it then binds to a GUI control on a ASP.NET WebForm.
My problem is that I need to filter the data returned - some of the data that is being returned should not be displayed to the user.
I'm aware of DataTable.select(), but that's not really what I need. First, it returns an array of DataRows, and I need a DataTable, so I can databind it to the GUI control. But more importantly, the filtering I need to do isn't something that can be easily put into a simple expression. I have an array of the elements which I do not want displayed, and I need to compare each element from the DataTable against that array.
What I could do, of course, is to create a new DataTable, reading everything out of the original, adding to the new what is appropriate, then databinding the new to the GUI control. But that just seems wrong, somehow. In this case, the number of elements in the original DataTable aren't likely to be enough that copying them all in memory is going to cause too much trouble, but I'm wondering if there is another way.
Does the .NET DataTable have functionality that would allow me to filter via a callback function?
What about binding the GUI Control To a List of DataRows which satisfies your condition? something like this:
var lst = new List<DataRow>();
foreach(DataRow dr in dt.Rows) {
if (SatisfiesCondition(dr)) lst.Add(dr);
}
// in Linq dialect
var lst = dt.AsEnumerable().Where(SatisfiesCondition).ToList();
// here: bind control to list
Doing it this way, the datarows won't be copied, but the list will keep the references to the rows you need.