private static readonly string[] strNumbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "zero", "ten" };
static void Main()
{
UseFirstMethod();
UseFirstOrDefaultMethod();
UseElementAt();
}
public static void UseFirstMethod()
{
Console.WriteLine("Use first to find element that start with 'o'");
var firstElement = strNumbers.First(word => word[0] == 'o');
Console.WriteLine(firstElement);
}
public static void UseFirstOrDefaultMethod()
{
var firstOrDefault = strNumbers.FirstOrDefault(word => word == "Guo");
Console.WriteLine("'Guo' {0} exist in numbers", firstOrDefault != null ? "" : "don't");
}
public static void UseElementAt()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var elementNumber =
(from number in numbers
where number > 5
select number).ElementAt(2);
Console.WriteLine("Use ElementAt to retrive third element more than 5");
Console.WriteLine(elementNumber);
}
LINQ Element Opreator
最新推荐文章于 2024-07-23 15:15:50 发布