从数组中获取所有元素,但第一个元素

时间:2022-12-09 21:16:20

Is there a one-line easy linq expression to just get everything from a simple array except the first element?

有没有一行简单的linq表达式来从一个简单的数组中获取除第一个元素之外的所有内容?

for (int i = 1; i <= contents.Length - 1; i++)
    Message += contents[i];

I just wanted to see if it was easier to condense.

我只是想看看是否更容易凝聚。

2 个解决方案

#1


187  

Yes, Enumerable.Skip does what you want:

是的,Enumerable.Skip做你想要的:

contents.Skip(1)

However, the result is an IEnumerable<T>, if you want to get an array use:

但是,如果要使用数组,结果是IEnumerable

contents.Skip(1).ToArray()

#2


6  

The following would be equivalent to your for loop:

以下内容相当于你的for循环:

foreach (var item in contents.Skip(1))
    Message += item;

#1


187  

Yes, Enumerable.Skip does what you want:

是的,Enumerable.Skip做你想要的:

contents.Skip(1)

However, the result is an IEnumerable<T>, if you want to get an array use:

但是,如果要使用数组,结果是IEnumerable

contents.Skip(1).ToArray()

#2


6  

The following would be equivalent to your for loop:

以下内容相当于你的for循环:

foreach (var item in contents.Skip(1))
    Message += item;