且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何采取在C#中,除了最后一个元素,所有数组元素

更新时间:2023-11-09 23:38:40

  VAR remStrings = queries.Take(queries.Length  -  1);

没有需要扭转,蹦跳。只要看看少了一个元素以外还有数组中

如果你真的想要以相反的顺序的元素,可以钉在一个 .Reverse()来结束。

I have a string array like this.

string[] queries with data more than one string.

I want to skip the last string from the element and take the remaining. I have come up with

var remStrings = queries.Reverse().Skip(1).Take(queries.Length - 1);

Is there a better alternative to this?

var remStrings = queries.Take(queries.Length - 1);

No need to Reverse and Skip. Just take one less element than there are in the array.

If you really wanted the elements in the reverse order, you could tack on a .Reverse() to the end.