Linq BoilerPlate: Is all of it needed?

↘锁芯ラ 提交于 2019-12-12 02:06:23

问题


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 DataRowViews from the view and select their underlying DataRows) 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!