score:1
the paragraph you showed from the book doesn't say it will print a single number. it says it will never print a single number.
[...] this program would never make it as far as printing out a single number [...]
indeed, deferred execution would allow you to run through an infinite sequence, while exeucting the full sequence without deferred exeuction would never end.
score:3
how can i define an extension method of where like the one in code 3 for the fibonacci example
you can use yield return
again if you want deferred execution.
public static ienumerable<cultureinfo> mywhere(this ienumerable<cultureinfo> source, predicate<cultureinfo> filter) {
foreach (var thing in source) {
if (filter(thing)) yield return thing;
}
}
note that it has to return and accept ienumerable<cultureinfo>
s because arrays can't be "infinite", ienumerable<t>
s can, because they can use deferred execution.
why would it print out a single number when the where method would never return?
if you try to call your customlinqprovider.where
method with an infinite ienumerable<cultureinfo>
, you'd first have to convert that to a cultureinfo[]
, which you will do by using toarray
. since the ienumerable<t>
is infinite, toarray
will go on forever. it's not that where
will not return, it's that toarray
will not.
also, the book says
this program would never make it as far as printing out a single number
"would never make it as far as" means "wouldn't even".
score:4
i defined an extension method as the following and it didn't print out anything. the point is that when it's going to be treated like an array then the where method would never return, hence the evaluation of evenfib would never end and we'll have nothing printed. i also made sure in my code when both linq to object provider and my own extension method are available, my extension method would be used. i didn't change code 1 and code 2 by the way.
public static t[] where<t>(this ienumerable<t> src,
predicate<t> filter)
{
var array = src.toarray();
return array.findall(array,filter);
}
Source: stackoverflow.com
Related Query
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Why does this linq extension method hit the database twice?
- What would be the Linq code for this grouping using VB.NET 2008?
- Why the LINQ method `Where` in my code is wrong for string[]?
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- What is the C# extension methods equivalent for this Linq query?
- LINQ - Writing an extension method to get the row with maximum value for each group
- Why I cannot use the Linq Where clause in a method of a public abstract class?
- How-to use predicates for customer where LINQ query in Entity Framework, Extension Method "Search by Words"
- Is LINQ Where extension method optimized for SortedDictionary?
- How to write this code using the Linq Extension Method-Syntax?
- Why is this Linq Where Clause not filtering the results
- What would be the LINQ solution for this query in c#?
- Extension method (like Take()) not available for the LINQ query, Any reason why?
- Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID
- Reduce the line of code for this LINQ query
- Linq extension method equivalent for that code in VB.NET
- Why do not my LINQ query print out data from the database?
- what would be the equivalent this for loop in linq
- How can I make Linq extension method available for the entities?
- Can't figure out the right linq query for this
- What would be the linq for this SQL Group Join
- Why is the Where clause in this LINQ Query not appending?
- What's the syntax for this linq expression calling the in method
- Finding and parsing the where clause in a method that has a LINQ expression for a parameter
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- Where is the "Fold" LINQ Extension Method?
- Why does the C# compiler go mad on this nested LINQ query?
- What is the performance of the Last() extension method for List<T>?
More Query from same tag
- How can I manipulate field properties dynamically?
- Execute the linq queryusing Where clause from string
- how do i refactor these linq properties?
- LINQ remove items with specific value in group by
- How to compare a List<long> with a comma-separated ids, using linq to entities
- What is the purpose of the EnumerableRowCollection<TRow> class?
- Pass a lambda as a variable to a .Select()
- How can two left joins from a single table on similar fields be translated into LINQ when more than two tables are involved?
- Create null instead of empty dictionary where IEnumerable has no data
- Wrong order when using System.Linq.Dynamic with MongoDB collection
- join with multiple group by element and select extra columns
- The result of a query cannot be enumerated more than once
- C# Linq XML Query where multiple elements of same name exist
- can not use navagation properties in web grid
- Getting a blob size without the blob itself
- LinqDataSource Select syntax
- LINQ to SQL: Left join on multiple columns
- Update data with LinQ to SQL not working
- Problem in populating a dictionary using Enumerable.Range()
- Iterator in aggregate (linq)?
- Add minutes where we have same datetime in list<DateTime>
- I am using LINQ to connect to a database and i get a list of properties and I want to convert it to an XML File
- C# linq Closest point from a specific route
- Set anonymous type to null
- linq using order by, where, and select vb.net
- How to connect to SQL Server Express DB from Server Explorer VS 2010?
- Is it possible to do a linq query on a GridView (ASP.NET)?
- LINQ Left join on nullable column giving null reference exception
- How to use count twice on same column in a table
- Unable to get double.TryParse to work in Linq Expression Tree