<分区>
我有一个 List<>
其中包含几个字符串。例如{a1, a2, a15, a3, a16, a1}
.我想获得最大数字 (a16) 的字符串。我尝试使用 Where() 和 Max() 但随后它将返回最大值 (16),我想在其中返回字符串 (a16)。
我想我可以用额外的 Where()
来做到这一点和 contains()
但我想知道我是否可以用更简单的方式来做。
这是代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace linq_example
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string> {"b1", "a1", "a2","a9", "a16", "a1", "a5" , "b20"};
var max = list.Where(i => i.IndexOf("a") != -1).Max(a => Convert.ToInt32(a.Substring(1)));
Console.WriteLine(max.ToString());
Console.ReadLine();
}
}
}
这个程序会在我想打印 a16 的地方打印 16