IEnumerable.Select索引 [英] IEnumerable.Select with index
本文介绍了IEnumerable.Select索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
var accidents = text.Skip(NumberOfAccidentsLine + 1).Take(numberOfAccidentsInFile).ToArray();
其中意外是一个字符串数组。
where accidents is an array of strings.
我想打从字符串数组一个LINQ转型意外对象的数组如下:
I want to make a Linq transformation from the string array to an array of Accident objects as follows:
return accidents.Select(t => new Accident() {Id = i, Name = t.Replace("\"", string.Empty)}).ToArray();
我如何从事故阵列使用LINQ检索索引我或我去老同学?
How do I retrieve the index i from the accidents array using Linq or do I have to go old school?
推荐答案
我不知道你要找什么样的指标为,但如果它只是设置连续的数字,那么你是幸运的。有选择
重载正是这么做的:
I'm not sure what kind of index you're looking for, but if it's just set of consecutive numbers then you're lucky. There is Select
overload that does exactly that:
return accidents.Select((t, i) => new Accident() {Id = i, Name = t.Replace("\"", string.Empty)}).ToArray();
这需要一个委托,它需要两个参数 - 该项目及其指数
It expects a delegate that takes two parameters - the item and its index.
这篇关于IEnumerable.Select索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文