I have the following code, which does work:
var dataSource = (from p in dv.ToTable().AsEnumerable() where filter(p) select p).AsDataView();
filter is a Func<DataRow, bool>
dv is a DataView
dataSource is being used as a DataSource for a DataGrid.
Anyhow, it strikes me as a bit ugly that I am calling ToTable, AsEnumerable, and AsDataView, so I was wondering if there is a way to reduce the number of calls.
Is this as simple as I can make it?
Edit: The DataGrid has pagination, and I make use of the dataSource to determine the total number of entries. I’m not especially worried about the efficiency of this; dv only has a few thousand items and the table is being maintained in memory.
Well for one thing, I’d say that using a query expression is somewhat clumsy here. Your code is equivalent to:
which I would say is clearer.
Another alternative would be:
This avoids building a
DataTable, so may well be more efficient too (it’ll just stream theDataRowViews from the view and select their underlyingDataRows) but builds aList<DataRow>at the end. On the other hand, it’s now not acting on the view itself, really – because it’s just selecting the underlying rows. This may or may not do what you need, depending on what your view is doing.