Single() 메서드는 조건을 만족하는 유일한 요소를 반환합니다. 이러한 요소가 두 개 이상 표시되면 오류가 발생합니다.
다음은 문자열 배열입니다.
string[] str = { "jack", "tom", "henry", "time"}; 이제 Single() 메서드를 사용하여 각 요소를 가져옵니다. 그런 다음 Lambda 식을 사용하여 길이가 4보다 큰 요소를 계산했습니다.
str.AsQueryable().Single(name => name.Length > 4);
예시
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] str = { "jack", "tom", "henry", "time"};
// finding string whose length is greater than 4
string res = str.AsQueryable().Single(name => name.Length > 4);
Console.WriteLine(res);
}
} 출력
henry