简体   繁体   English

C#如何从IEnumerable中列出

[英]C# How to make a list out of IEnumerable

I'm currently playing with TvDbSharper ( https://github.com/HristoKolev/TvDbSharper ) and i have a question about IEnumerable. 我目前正在玩TvDbSharper( https://github.com/HristoKolev/TvDbSharper ),并且我对IEnumerable有疑问。 What i'm trying to do is get all the Id for a given series and then add the result in a listView. 我想做的是获取给定系列的所有ID,然后将结果添加到listView中。

This code gives me the first Id in the list: 这段代码为我提供了列表中的第一个ID:

const int SeriesId = 78804;
var tasks = new List<Task<TvDbResponse<BasicEpisode[]>>>();
var firstResponse = await client.Series.GetEpisodesAsync(SeriesId, 1);
for (int i = 2; i <= firstResponse.Links.Last; i++)
{
tasks.Add(client.Series.GetEpisodesAsync(SeriesId, i));
}
var results = await Task.WhenAll(tasks);
var episodes = firstResponse.Data.Concat(results.SelectMany(x => x.Data));

epsListview.View = View.Details;
epsListview.Columns.Add("Episode", 100);
string[] arr = new string[4];
ListViewItem itm;
arr[0] = episodes.First().Id.ToString();
itm = new ListViewItem(arr);
epsListview.Items.Add(itm);

but what i want is to make a new line in the epsListview for each id available. 但是我想要的是在epsListview中为每个可用ID换行。

I never used IEnumerable and i'm still very novice using c#, i've been stuck with this problem for over a week now. 我从来没有使用过IEnumerable,但我还是使用c#的新手,我已经被这个问题困扰了一个多星期了。 can anyone help me with this ? 谁能帮我这个 ?

Thanks!! 谢谢!!

It looks like you're wanting to build an array of IDs from an IEnumerable of episodes, and add that array to a single ListViewItem. 看来您想从IEnumerable情节构建ID数组,并将该数组添加到单个ListViewItem中。 This should do that. 这应该做到这一点。

string[] arr = episodes.Select(episode => episode.Id.ToString()).ToArray()
ListViewItem itm = new ListViewItem(arr);
epsListview.Items.Add(itm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM