Can anyone help me?
I have a list ( listA
), and I have another one ( listB
)
I want to save all elements of listA
in listB
except the first element.
Example: if listA value is {1, 2, 3, 4, 5}
, then listB is {2, 3, 4, 5}
.
Now, I have this code:
listB = listA;
listB.RemoveAt(0);
But I want to write this code in one line; is there any way to doing that?
You can use Skip
method to achive that.
var listB = listA.Skip(1).ToList();
It will skip the first element in the list and create a new List
change the listB = listA;
to
listB = listA.Skip(1).ToList();
Yes, there is. You may use System.Linq 's Enumerable.Skip .
That would make your code: listB = listA.Skip(1).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.