I would like to sort my int array in ascending order.
first I make a copy of my array:
int[] copyArray = myArray.ToArray();
Then I would like to sort it in ascending order like this:
int[] sortedCopy = from element in copyArray
orderby element ascending select element;
But I get a error, “selected” gets highligted and the error is:
“cannot implicitly convert type ‘system.linq.iorderedenumerable’ to ‘int[]'”
You need to call
ToArray()at the end to actually convert the ordered sequence into an array. LINQ uses lazy evaluation, which means that until you callToArray(),ToList()or some other similar method the intermediate processing (in this case sorting) will not be performed.Doing this will already make a copy of the elements, so you don’t actually need to create your own copy first.
Example:
It would perhaps be preferable to write this in expression syntax: