I have a List <string[]>
.
What is the simplest way to trim all strings using LINQ?
LINQ is for querying, It shouldn't be used for modifying existing collection. You can use the following, but it will return a new collection.
List<string[]> newList = list.Select(outer => outer
.Select(innerItem => innerItem.Trim())
.ToArray())
.ToList();
You may add checking against Null
for each element in the string array before calling Trim
to avoid NRE. Something like:
.Select(innerItem => innerItem != null ? innerItem.Trim() : null)
If the original signature is correct it should be
var result = original
.Select( x=>
x.Select(y => y.Trim())
.ToArray())
.ToList();
where x is each array of Strings in the original list, and y is the member from the inner array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With