是否可以将 IEnumerable 列表转换为 BindingList 集合?
IEnumerable 列表是类型化对象的列表,例如:
IEnumerable<AccountInfo> accounts = bll.GetAccounts(u.UserName, u.Password);
我的 PagingList 只是扩展了 BindingList:
public class PagingList<T>
{
public BindingList<T> Collection { get; set; }
public int Count { get; set; }
public PagingList()
{
Collection = new BindingList<T>();
Count = 0;
}
}
我只是想将我的 IEnumerable 列表传递给一个使用 PagingControl 呈现列表的方法:
protected void RenderListingsRows(PagingList<AccountInfo> list)
{
foreach (var item in list)
{
//render stuff
}
}
但似乎我无法在两者之间进行转换,谁能指出我遗漏了什么?!
非常感谢
本
最佳答案
需要指出的是,您的 PagingList 没有扩展 BindingList,而是通过组合来使用它。
我在寻找类似答案时遇到了这个问题。这里的答案似乎都没有为您的问题提供明确的解决方案,尽管他们在解决问题时提到了有值(value)的观点。我想我会为路过的人添加一个。
鉴于所提供的信息,简单的答案是否定的,但无需重构类即可满足您的需求的简单解决方案是:
IEnumerable<AccountInfo> accounts= bll.GetAccounts(u.UserName, u.Password);
myPagingList.Collection = new BindingList<Foo>(myfoos.ToList());
因此,您必须将您的 AccountInfo 项物理添加到 BindingList 实例属性“Collection”中。
关于c# - 为什么我不能将 IEnumerable<T> 列表转换为 BindingList<t>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1070107/