如何在集合中找到一定长度的元素? [英] How to find the element of certain length in a collection?
本文介绍了如何在集合中找到一定长度的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个名称集合,可能有10个字符或更长的名称,返回此名称,如果没有找到则返回null,如果有更多的b / b
则抛出异常。 />
Given a collection of names, there may be a name 10 characters or longer, return this name, or null if none is found,throw an exception if there is more
than 1.
public static string GetLongestName(IEnumerable<string> names)
{
throw new NotImplementedException();
}
我的尝试:
请推荐IDE或链接,因为我无法访问我的工作电脑。我曾尝试下载c#IDE但无法正常工作。
What I have tried:
please recommend an IDE or a link, as I can't reach my work pc. I have tried to download c# IDE but did not work correctly.
推荐答案
IDE:Visual Studio。你可以为微软下载几个Express / Community版本 - 谷歌会为你找到它们。
然后:
IDE: Visual Studio. You can download several Express / Community versions for Microsoft - google will find them for you.
Then:
public static string GetLongestName(IEnumerable<string> names)
{
var all = names.Where(s => s.Length >= 10).OrderByDescending(s => s.Length);
if (all.Count() <= 1) return all.FirstOrDefault();
throw new ApplicationException("Too many strings match the criteria");
}
这篇关于如何在集合中找到一定长度的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文