I'm trying to solve one task with .Net 3.1 and Linq and can't succeed.
I have function which accepts one parameter type of IEnumerable and returns same type of value:
IEnumerable<string> Foo(IEnumerable<string> strCollection){}
The input parameter (strCollection) is always sorted in ascending order.
I need to select the last character from each string, converting it to uppercase, and from the received characters, compose a string.
Returned collection depends on strCollection[i].Length. Say, if strCollection contains 3 elements with length of 2 and 2 elements with length of 3, the retuned collection should have 2 elements.
Then I should arrange the resulting sequence of strings in descending order of their lengths.
Say,
if(1):
strCollection = new[] { "bc", "sd", "ac", "sdf", "ewr" };
Function should return:
new[] { "CCD", "RF" });
if(2):
strCollection = new[] { "ab", "attribute", "cheese", "swim", "cut" };
return:
new[] { "B", "E", "E", "T", "M" });
if(3):
strCollection = new[] { "du", "the", "ace", "run", "cut" };
return:
new[] { "ETNE", "U" });
if(4):
strCollection = new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" };
return:
new[] { "YTR", "D", "T", "E", "R" });
Here is my solution which does not works:
IEnumerable<string> Foo(IEnumerable<string> strCollection)
{
return stringList.OrderBy(x => x.Length).GroupBy(x => x.Length).Select(x => x.Key).Reverse().Select(x => x.ToString());
}
Please help!