c# - Linq 返回 IEnumerable<MyObject>,如何返回 MyListObject?

标签 c# .net linq

有没有一种方法可以不循环所有 IEnumerable 来取回对象(继承 BindingList)中的数据?

MyListObject--> Transformed with Linq --> Back the data inside MyListObject

我知道我可以做 .ToList,但它不符合我的意愿。

有什么想法吗?谢谢:)

最佳答案

我不确定您的具体要求是什么,尤其是关于 ToList 的部分没有做你需要的。

不幸的是,BindingList<T>只接受 IList<T>其构造函数中的参数,而不是 IEnumerable<T> .

但是,如果您在自定义集合上实现传递构造函数(或者如果它已经有一个采用 IList<T> 的构造函数),那么您可以执行类似的操作:

public class MyListObject<T> : BindingList<T>
{
    public MyListObject() : base() { }
    public MyListObject(IList<T> list) : base(list) { }
}

// ...

MyListObject<int> yourList = new MyListObject<int> { 1, 2, 3, 4, 5 };
yourList = new MyListObject<int>(yourList.Select(s => s * 2).ToList());
// yourList now contains 2, 4, 6, 8, 10

关于c# - Linq 返回 IEnumerable<MyObject>,如何返回 MyListObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/555320/

相关文章:

c# - Spring MVC 中的 Response.Clear 和 Response.ClearContent

c# - 如何在 ASP.NET Core 中提供正在写入的文件

c# - 动态构建连接字符串

c# - .net中异步调用和异步io调用的区别

c# - MS Project : where to get started? 的 super 简约插件

c# - 按照可配置的时间表运行任务的最有效方式

linq - MVC3 Razor @Html.DropDownListFor

.net - 线程试图读取或写入它没有适当访问权限的虚拟地址

c# - Linq to NHibernate 返回名称以字符串列表内容开头的实体

c# - 我怎样才能 "decode"C# 中的 `&#231;` 值