I have a variable that contains :
Sometimes array of strings and sometimes array of array of strings
e.g:
var words = a LINQ to XML query;
//words == { "01", "02", "03", ... }
//or
//words == { {"01", "02", "03"}, {"04", "05", "06"}, ... }
Now , I wanna access to each item in the words array with a for statement
Could you please guide me ?
Thanks
(Note: please see the edit…)
If you don’t actually know beforehand whether you’ve got a string array or an array of arrays of strings, you’ll need to conditionally call
SelectMany:Note that in this case if you change the contents of
flattened, that will either changewords(if that was already flat) or just change a copy (if it was a jagged array). If you want to make it consistent, you could callwords.ToArray()in second operand of the conditional operator:Is there no way you can avoid the ambiguity to start with though? In particular, if you’re in control over the LINQ to XML queries involved, you should be able to make them give a consistent result type.
EDIT: I’ve just realised that if you don’t know the type of the data to start with, then
wordsis presumably declared as justobjector perhapsIEnumerable. In that case, you’d want something like this:This is still really icky – you should definitely try to make the queries give you some consistent result type instead.