Articles → .NET → Deferred And Immediate Execution In LINQ

Deferred And Immediate Execution In LINQ






Deferred Execution




List<string> weekdays = new List<string>();
weekdays.AddRange(new string[] 
            { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"});

var weekdayWithT = from element in weekdays where element.StartsWith("t") select element;

foreach (var el in weekdayWithT)
{
     MessageBox.Show(el.ToString());
}




Picture showing the output of deferred execution in LINQ
Click to Enlarge




Immediate Execution




List<string> weekdays = new List<string>();
weekdays.AddRange(new string[] 
{ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"});

int weekdayWithTCount = (from element in weekdays where element.StartsWith("t") select element).Count();
MessageBox.Show(weekdayWithTCount.ToString());


Picture showing the output of immediate execution in LINQ
Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Monday, January 18, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250