Which of the following LINQ fucntion must return only one record?
.Single() must return a single record, or it will throw an error if no records found, or more than one record found.
using System;
using System.Linq;
class Program
{
static void Main()
{
// Find only element > 999.
int[] array1 = { 1, 3, 1000, 4, 5 };
int value1 = array1.Single(element => element > 999);
// Ensure only one element.
int[] array2 = { 4 };
int value2 = array2.Single();
Console.WriteLine(value1);
Console.WriteLine(value2);
// See exception when more than one element found.
try
{
int value3 = array1.Single(element => element > 0);
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType());
}
}
}
Output
------
1000
4
System.InvalidOperationException