English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Consulta de Execução Imediata LINQ

Execução Imediata é o oposto da Execução Diferiada. Ela força a execução da consulta LINQ e obtem os resultados imediatamente. O operador de conversão “To” executa a consulta fornecida e dá os resultados imediatamente.

Sintaxe de Métodos

No exemplo a seguir, o método ToList() executa a consulta imediatamente e retorna os resultados.

 C#:Executar Imediatamente

IList<Student> teenAgerStudents = 
                studentList.Where(s => s.age > 12 && s.age < 20).ToList();

 VB.Net:Executar Imediatamente

Dim teenAgerStudents As IList(Of Student) = 
                    studentList.Where(Function(s) s.Age > 12 E s.Age < 20).ToList()

Sintaxe de Consulta

C#:
var teenAgerStudents = from s in studentList
                where s.age > 12 && s.age < 20
                select s;

A consulta acima não será executada imediatamente. Você não encontrará nenhum resultado, conforme mostrado a seguir:

Executar Imediatamente

A sintaxe de consulta não suporta o operador “To”, mas pode usar ToList(), ToArray() ou ToDictionary() para execução imediata, conforme mostrado a seguir:

C#:
IList<Student> teenAgerStudents = (from s in studentList
                where s.age > 12 && s.age < 20
                select s).ToList();
VB.Net:
Dim teenAgerStudents As IList(Of Student) = (From s In studentList _
                Onde s.Age > 12 E s.Age < 20 _
                Select s).ToList()

Você pode ver os resultados na coleção teenAgerStudents, conforme mostrado a seguir:

Executar Imediatamente