我试图用.Net 3.1
和Linq
来解决一项任务,但不能成功.
我有一个函数,它接受一种类型的IEnumerable参数,并返回相同类型的值:
IEnumerable<string> Foo(IEnumerable<string> strCollection){}
输入参数(strCollection
)总是按升序排序.
我需要从每个字符串中 Select 最后一个字符,将其转换为大写,并从收到的字符中组成一个字符串.
返还的Collection 品取决于strCollection[i].Length
.比方说,如果strCollection
包含3个长度为2的元素和2个长度为3的元素,则返回的集合应该有2个元素.
然后,我应该按照字符串长度的降序排列得到的字符串序列.
说,
If(1):
strCollection = new[] { "bc", "sd", "ac", "sdf", "ewr" };
函数应返回:
new[] { "CCD", "RF" });
If(2):
strCollection = new[] { "ab", "attribute", "cheese", "swim", "cut" };
返回:
new[] { "B", "E", "E", "T", "M" });
If(3):
strCollection = new[] { "du", "the", "ace", "run", "cut" };
返回:
new[] { "ETNE", "U" });
If(4):
strCollection = new[] { "star", "galaxy", "quasar", "planet", "asteroid", "satellite", "comet" };
返回:
new[] { "YTR", "D", "T", "E", "R" });
以下是我的不起作用的解决方案:
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());
}
请救救我!