问题
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.
回答1:
Well for one thing, I'd say that using a query expression is somewhat clumsy here. Your code is equivalent to:
var dataSource = dv.ToTable()
.AsEnumerable()
.Where(filter)
.AsDataView();
which I would say is clearer.
Another alternative would be:
var dataSource = dv.Cast<DataRowView>()
.Select(rowView => rowView.Row)
.Where(filter)
.ToList();
This avoids building a DataTable
, so may well be more efficient too (it'll just stream the DataRowView
s from the view and select their underlying DataRow
s) but builds a List<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.
回答2:
It looks like you want:
var dataSource = dv.ToTable().AsEnumerable().Where(filter);
AsEnumerable
is required for DataTable
and returns an IEnumerable<DataRow>
which DataTable
does not implement.
来源:https://stackoverflow.com/questions/5236866/linq-boilerplate-is-all-of-it-needed