I have the following strings in a List<string>
vs0, vs1, vs2, vs3, vs4, vs5, ... vs(n)
In my list, they are not sorted and are random. I want to get the string which has the highest int
in it. And then get the number out of that string into a int var.
What is the best and fastest way do this?
var max = myList.OrderByDescending(v => int.Parse(v.Substring(2))).First();
或者如果你需要最高的int
var max = myList.Select(v => int.Parse(v.Substring(2))).Max();
var result = List.Max(p => int.Parse(p.Substring(2)));
How about with LINQ:
List<string> strings = ...
int max = strings.Max(item => int.Parse(item.Substring(2)));
Perhaps just l.Select(Convert.ToInt32).Max()
?
Per Jim's suggestion, need to strip the 2 leading characters:
l.Max(s => Convert.ToInt32(s.Substring(2)))
(but this one is practically the same as other answers).
var max = yourList.Select(s => int.Parse(s.Substring(2)).Max();
l.Select(s => int.Parse(s.Substring(2))).Max()
如果您计划多次执行此操作,则可能需要创建单独的int
列表
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.