쿼리 식에서는 후속 절에서 사용하기 위해 하위 식의 결과를 저장하는 것이 유용할 수 있습니다. 키워드를 let
사용하여 이 작업을 수행할 수 있습니다. 이 키워드는 새 범위 변수를 만들고 사용자가 제공한 식의 결과로 초기화합니다. 값으로 초기화되면 범위 변수를 사용하여 다른 값을 저장할 수 없습니다. 그러나 범위 변수에 쿼리 가능한 형식이 있는 경우 쿼리할 수 있습니다.
예시
다음 예제 let
에서는 두 가지 방법으로 사용됩니다.
자체 쿼리할 수 있는 열거 가능한 형식을 만들려면
쿼리가 범위 변수
ToLower
에서word
를 한 번만 호출하도록 설정하려면.let
를 사용하지 않으면,ToLower
절의 각 조건자에서where
을 호출해야 합니다.
class LetSample1
{
static void Main()
{
string[] strings =
[
"A penny saved is a penny earned.",
"The early bird catches the worm.",
"The pen is mightier than the sword."
];
// Split the sentence into an array of words
// and select those whose first letter is a vowel.
var earlyBirdQuery =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0] == 'a' || w[0] == 'e'
|| w[0] == 'i' || w[0] == 'o'
|| w[0] == 'u'
select word;
// Execute the query.
foreach (var v in earlyBirdQuery)
{
Console.WriteLine($"\"{v}\" starts with a vowel");
}
}
}
/* Output:
"A" starts with a vowel
"is" starts with a vowel
"a" starts with a vowel
"earned." starts with a vowel
"early" starts with a vowel
"is" starts with a vowel
*/
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET