masoud_sedighy@yahoo.com (11) [Avatar] Offline
#1
Hello

In figure 3.2 (page 96) has been mentioned where operator returns a filtered list but in page 190 about performance consideration has been mentioned where operator does like below and it seems it return just one item, I was thinking at first filtered the lines and then process continues. Is it possible explaining more because I cannot relate these concepts to each other?

1 A loop starts, using the Lines operator to read a line from the file.
a. If there are no more lines to deal with, the process halts.
2 The Where operator executes on the line.
a. If the line starts with #, itÂ’s a comment so the line is skipped. Execution
resumes at step 1.
b. If the line is not a comment, the process continues.

3 The line is split into parts.
4 An object is created by the Select operator.
5 Work is performed on the book object as specified in the body of the
foreach statement.
6 The process continues at step 1.

Best regards.
fabrice.marguerie (224) [Avatar] Offline
#2
Re: misunderstanding between standard query operator & differed query execution
Hello,

All of this is correct. Both on page 96 and on page 190.
As is noted on page 96: "Each element in the sequence is processed only when it is requested. This is how delayed execution works. The elements in sequences are not processed in batch, and maybe even not all processed if not requested.".

Items are pulled one by one, when needed and only if needed.

Fabrice
masoud_sedighy@yahoo.com (11) [Avatar] Offline
#3
Re: misunderstanding between standard query operator & differed query execution
Thank you very much Fabrice. I am new to LINQ. About 1 month ago I get started learning linq with your book and I found this is very helpful so I try to read line by line.
Actually my question comes after comparing these two codes. In sample one we have not used (.select operator) so I was thinking (.where) operator filters in batch. I like to know where operator in sample 1 how works without (select operator) because until now I was thinking
select operator=request
Please help where is my misunderstanding.
Thanks in advanced.

Sample 1:
Listing 5.25 Using the ForEach query operator using the method syntax
SampleData.Books
.Where(book => book.PageCount > 150)
.ForEach(book => Console.WriteLine(book.Title));

Sample 2:
Listing 5.26 Using the ForEach query operator with a query expression
(from book in SampleData.Books
where book.PageCount > 150
select book)
.ForEach(book => Console.WriteLine(book.Title));
fabrice.marguerie (224) [Avatar] Offline
#4
Re: misunderstanding between standard query operator & differed query execution
Select (or select) does not perform the enumeration operation. In the samples you give, ForEach is the operator performing the enumeration operation.
I sample 2, select is here just because it is required in a C# query expression. You'll note that it's not the case in VB...

The select operator is actually useful only when we use something like select book.Title or select book.Price / 2.

Select is part of the query definition but does not execute the query. Iteration happens only with foreach, ForEach, ToArray, ToList and the likes.
masoud_sedighy@yahoo.com (11) [Avatar] Offline
#5
Re: misunderstanding between standard query operator & differed query execution
Thank you very much. Now I understood and it removed big problem of me when I read the book.