启用LINQ的BindingListView< T> [英] Enabling LINQ for BindingListView<T>

查看:92
本文介绍了启用LINQ的BindingListView< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安德鲁·戴维斯创建了 sourceforge上名为 BindingListView< T> 基本上可以让你收集到 DataGridView的绑定,同时支持排序和过滤。绑定 DataGridView的到正常列表< T> 不支持排序和筛选,为正确的接口不落实通过列表< T>

Andrew Davies created an excellent little class on sourceforge called BindingListView<T> which essentially allows you to bind a collection to a DataGridView while supporting sorting and filtering. Binding a DataGridView to a normal List<T> does not support sorting and filtering, as the proper interfaces are not implemented by List<T>.

类的伟大工程,解决了我的UI问题。然而,这将会是真棒,如果我可以通过使用LINQ集合迭代,但我只是不知道如何设置它这样做。源代码可以在这里下载 。 ?谁能帮我出

The class works great and has solved my UI problems. However, it'd be awesome if I could iterate through the collection using LINQ, but I'm just not sure how to set it up to do so. Source code can be downloaded here. Can anyone help me out?

推荐答案

由于该 BindingListView< T> 项目采用的.NET Framework 2.0和LINQ早,它不公开的的IEnumerable< T> 为您查询的。由于它的实施非通用的IEnumerable 和非通用的IList ,您可以使用 Enumerable.Cast< TResult> 来集合转换成适合与LINQ使用的形式。 T> 返回的是一个内部数据结构,但是,因为的IEnumerable AggregateBindingListView<这种做法是不是有帮助键入 KeyValuePair< ListItemPair< T> ;, INT方式>

Because the BindingListView<T> project uses .NET Framework v2.0 and predates LINQ, it doesn't expose an IEnumerable<T> for you to query on. Since it does implement non-generic IEnumerable and non-generic IList, you can use Enumerable.Cast<TResult> to convert the collection into a form suitable for use with LINQ. However, this approach isn't that helpful because the IEnumerable that AggregateBindingListView<T> returns is an internal data structure with type KeyValuePair<ListItemPair<T>, int>.

要升级此项目为使用LINQ方便的使用,最简单的办法可能是实施的IEnumerable< T> AggregateBindingListView< T> 。首先,它添加到类的声明:

To upgrade this project for convenient use with LINQ, the simplest approach might be to implement IEnumerable<T> on AggregateBindingListView<T>. First add it to the declaration of the class:

public class AggregateBindingListView<T> : Component, IBindingListView, IList, IRaiseItemChangedEvents, ICancelAddNew, ITypedList, IEnumerable<T>



,然后在类定义年底实现:

and then implement it at the end of the class definition:

#region IEnumerable<T> Members

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
    for (int i = 0; i < _sourceIndices.Count; i++)
        yield return _sourceIndices[i].Key.Item.Object;

}

#endregion

和现在

// Create a view of the items
itemsView = new BindingListView<Item>(feed.Items);
var descriptions = itemsView.Select(t => t.Description);



记住所有的.NET框架2.0版的升级项目到.NET Framework 4客户端配置文件和添加使用System.Linq的;为了这与当前项目工作

这篇关于启用LINQ的BindingListView&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆