I am trying to write a linq query.
I need my query to take a 2 dimensional array (d). It needs to check for each array within d if the first and last elements are not empty but any element within is empty, it will remove the empty element.
Example
{{test,"",test}, {test, test, test,"",test}} = {{test,test}, {test, test, test,test}}
But I need to be able to keep empty strings so cannot just search for them and remove them.
I hope I have explained that well enough.
I would try this:
string test = "test";
var array = {{test,"",test}, {test, test, test,"",test}};
string[] TransformRow(string[] inputRow)
{
return intputRow.Where(cell => cell != string.Empty).ToArray();
}
var validArray = array.Select(row => TransformRow(row));
Maybe
var out = input.Select(x =>
((!String.IsNullOrEmpty(x.FirstOrDefault()))
&& (!String.IsNullOrEmpty(x.LastOrDefault())))
? x.Where(y => !String.IsNullOrEmpty(y)).ToArray()
: x).ToArray();
If you only need IEnumerables you can probably remove the ToArrays.
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.