given a collection of names, there may be a name that is 10 characters or longer, return this name or null if none found, throw and exception if there is more than 1
public static string GetLongestName(IEnumerable names)
{
//code
}
I am new to linq and c# . please help.
Er Pratap JadhwarPosted Jul 16, 2017, 1:12 PM
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
var strings = new string[] { "1", "02", "003", "0004", "00005", "000006", "0000007", "00000008", "000000009", "0000000010" };
string longest = strings.Where(s => s.Length >= 10).SingleOrDefault();
Console.WriteLine("Longest text is :" + longest);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
}
Roy GuzmanPosted Jul 17, 2017, 9:09 AM