文章内容

2017/5/23 9:25:10,作 者: 黄兵

c# ienumerable 添加到list

I want to convert from IEnumerable<Contact> to List<Contact>. How can I do this?

我想从转换IEnumerable<Contact>转换List<Contact>我该怎么做?

解决方案:

You can do this very simply using LINQ.

Make sure this using is at the top of your C# file:

using System.Linq;

Then use the ToList extension method.

Example:

IEnumerable<int> enumerable = Enumerable.Range(1, 300);
List<int> asList = enumerable.ToList();

another way

List<int> list=new List<int>();

IEnumerable<int> enumerable =Enumerable.Range(1, 300);  

foreach (var item in enumerable )  
{     
  list.add(item);  
}

你可以简单的使用LINQ来做到这一点。

确保此使用位于C#文件的顶部:

using System.Linq;

然后使用ToList扩展方法。

例:

IEnumerable<int> enumerable = Enumerable.Range(1, 300);
List<int> asList = enumerable.ToList();



分享到:

发表评论

评论列表