是否有获得最长的字符串中的字符串列表的LINQ功能? [英] Is there a LINQ function for getting the longest string in a list of strings?
本文介绍了是否有获得最长的字符串中的字符串列表的LINQ功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有这样的 LINQ
函数或人会编写它自己是这样的:
静态字符串GetLongestStringInList()
{
线最长=列表[0];
的foreach(列表中的字符串s)
{
如果(s.Length> longest.Length)
{
最长= S;
}
}
返回最长的;
}
解决方案
这将只有做到这一点一个循环迭代:
list.Aggregate(,(最大,当前)=>最大长度> CUR。 ?最大长度:CUR);
Is there a LINQ
function for this is or would one have to code it themselves like this:
static string GetLongestStringInList()
{
string longest = list[0];
foreach (string s in list)
{
if (s.Length > longest.Length)
{
longest = s;
}
}
return longest;
}
解决方案
This will do it with only one loop iteration:
list.Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur);
这篇关于是否有获得最长的字符串中的字符串列表的LINQ功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文