c# - 为什么我不能将 IEnumerable<T> 列表转换为 BindingList<t>?

标签 c# ienumerable bindinglist

是否可以将 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/

相关文章:

c# - 空字符串上的数据触发

c# - 遍历列表并修改对象正在更新原始对象

c# - 无法将 IList 转换为绑定(bind)列表

c# - 模拟管理员可以控制网页上某些用户输入字段的只读/查看/默认设置的场景?

c# - response.writefile 后页面处理停止

c# - 仅使用 LINQ 时使用 IEquatable 比较两个集合

c# - IEnumerable<T>/IQueryable<T> 上的动态 LINQ OrderBy

c# - 从 BindingList 中删除重复项

c# - unsigned char * - 等效的 C#

c# - 为什么 Enumerator.MoveNext 在与 using 和 async-await 一起使用时不能像我预期的那样工作?