Click here to Skip to main content
15,881,380 members
Home / Discussions / C#
   

C#

 
GeneralRe: IEnumerable OrderBy on a text field Pin
manognya kota6-Jan-12 0:58
manognya kota6-Jan-12 0:58 
GeneralRe: IEnumerable OrderBy on a text field Pin
manognya kota6-Jan-12 1:23
manognya kota6-Jan-12 1:23 
GeneralRe: IEnumerable OrderBy on a text field Pin
__John_6-Jan-12 1:59
__John_6-Jan-12 1:59 
GeneralRe: IEnumerable OrderBy on a text field Pin
manognya kota6-Jan-12 2:12
manognya kota6-Jan-12 2:12 
GeneralRe: IEnumerable OrderBy on a text field Pin
Vincent Blais6-Jan-12 2:35
professionalVincent Blais6-Jan-12 2:35 
GeneralRe: IEnumerable OrderBy on a text field Pin
__John_6-Jan-12 2:47
__John_6-Jan-12 2:47 
GeneralRe: IEnumerable OrderBy on a text field Pin
__John_6-Jan-12 3:12
__John_6-Jan-12 3:12 
GeneralRe: IEnumerable OrderBy on a text field Pin
Vincent Blais6-Jan-12 4:00
professionalVincent Blais6-Jan-12 4:00 
__John_ wrote:


It uses a 'lazy' strategy, ie. only evaluating an expression or executing a function when the result is actually needed, am I right?


Yes you are right. It's a principle of Linq to defer execution until is needed. And Linq also evaluate only the elements needed to return the result
Let's take Wayne List and do some examples

List<Person> peoples = new List<Person> {new Person("wayne"), new Person("sarah"), new Person("mark"), new Person("simon"), new Person("ashleigh"), new Person("dave"), new Person("connor"), new Person("bronwyn"), new Person("chantelle"), new Person("will"), new Person("chris")};

int Count = people.Count(p => p.Name.StartsWith("w")); //<-- Count is a greedy operator and all persons are evaluated for a result of 2.

var firstperson = people.Where(p => p.Name.Length == 5).Take(3);
foreach (var p in firstperson) // <-- evaluation start where and only Wayne, Sarah, Mark and Simon will be evaluated. The rest of the list is left alone
{
   Console.WriteLine(p.Name);
}

// And to show you when Linq expression are evaluated, try 
var firstperson2 = people.Where(p => p.Name.Length == 5).Take(3);
people.Insert(1, new Person("Vince"));

foreach (var p in firstperson) // <-- evaluation start where and only Wayne, Vince ans Sarah, will be evaluated. The rest of the list is left alone
{
    Console.WriteLine(p.Name);
}


http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx[^]


__John_ wrote:
BTW: How can I enumerate the results more that once?

If you use a Enumerator<t>, you can use Reset to set the enumerator to its initial position, which is before the first element in the collection.
But if you use a foreach loop, you can reuse an Enumerable<t> many times. The foreach loop will start at the beginning every time.
Vince

Remember the dead, fight for the living

GeneralRe: IEnumerable OrderBy on a text field Pin
__John_6-Jan-12 4:12
__John_6-Jan-12 4:12 
GeneralRe: IEnumerable OrderBy on a text field Pin
Wayne Gaylard6-Jan-12 20:55
professionalWayne Gaylard6-Jan-12 20:55 
GeneralRe: IEnumerable OrderBy on a text field Pin
manognya kota9-Jan-12 22:00
manognya kota9-Jan-12 22:00 
QuestionWhat is the memory limitation of C# Application Pin
NavpreetSingh5-Jan-12 19:44
NavpreetSingh5-Jan-12 19:44 
AnswerMessage Closed Pin
5-Jan-12 21:21
leddoes5-Jan-12 21:21 
GeneralRe: What is the memory limitation of C# Application Pin
RobCroll6-Jan-12 0:19
RobCroll6-Jan-12 0:19 
AnswerRe: What is the memory limitation of C# Application Pin
OriginalGriff5-Jan-12 21:44
mveOriginalGriff5-Jan-12 21:44 
GeneralRe: What is the memory limitation of C# Application Pin
RedDk29-Sep-23 6:52
RedDk29-Sep-23 6:52 
GeneralRe: What is the memory limitation of C# Application Pin
OriginalGriff29-Sep-23 8:09
mveOriginalGriff29-Sep-23 8:09 
GeneralRe: What is the memory limitation of C# Application Pin
RedDk29-Sep-23 9:01
RedDk29-Sep-23 9:01 
AnswerRe: What is the memory limitation of C# Application Pin
Luc Pattyn6-Jan-12 2:10
sitebuilderLuc Pattyn6-Jan-12 2:10 
QuestionWhat Type of Application to Choose Pin
NavpreetSingh5-Jan-12 18:18
NavpreetSingh5-Jan-12 18:18 
AnswerRe: What Type of Application to Choose Pin
Sentenryu5-Jan-12 22:23
Sentenryu5-Jan-12 22:23 
AnswerRe: What Type of Application to Choose Pin
BillWoodruff5-Jan-12 23:20
professionalBillWoodruff5-Jan-12 23:20 
AnswerRe: What Type of Application to Choose Pin
RobCroll6-Jan-12 0:39
RobCroll6-Jan-12 0:39 
AnswerRe: What Type of Application to Choose Pin
PIEBALDconsult6-Jan-12 2:22
mvePIEBALDconsult6-Jan-12 2:22 
Questionhow to register the USB HID KEYBOARD for notification using c# in windows OS(xp,vista,SEVEN-- [for all--- 32 and 64 bit]) Pin
Member 85373615-Jan-12 17:05
Member 85373615-Jan-12 17:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.