I have the following code:
var accidents = text.Skip(NumberOfAccidentsLine + 1).Take(numberOfAccidentsInFile).ToArray();
这里 accidents 是一个字符串数组。
我想要将字符串数组通过 Linq 转换为 Accident 对象数组,如下所示:
return accidents.Select(t => new Accident() {Id = i, Name = t.Replace("\"", string.Empty)}).ToArray();
我该如何使用Linq从事故数组中检索索引i,还是必须老派地去做?
accidents
数组本身获取索引,还是从原始的text
枚举中获取索引?如果是前者,那么Marcin的答案就可以了。如果是后者,那么你需要在最初引入索引的Select()
之后进行Skip()
和Take()
操作。 - Peter Duniho