3 ms·
Absolutely. You saved me writing the same thing. Regarding point 1, I would add there are also things that can be done using the query syntax that simply can't
by upthedale 14y ago
Absolutely. You saved me writing the same thing.
Regarding point 1, I would add there are also things that can be done using the query syntax that simply can't with the extension methods. For instance, I'm not sure how you would scope things in the same way as the 'let' clause when using extension methods.
Regarding the other points, I would only add that deferred execution and lazy evaluation are things to be embraced, not hidden away and overriden. Should we also not bother with the yield keyword?
Of course, there's always some occasion where you'll want to ToList your IEnumerable. I'd argue this should be of concern to the consumer of the IEnumerable though, not the producer.
Point 5 at least tries to allude to something useful. Given Linq's grounding in functional programming, of course you want to try to avoid side effects.
- SimonB86 14y agoRegarding the let keyword; the following code: var x = from post in posts let keywords = post.split(' ') ... Is compiled* into: var x = posts .Select(post => new { keywords = post.split(' '), post }) ... * If you didn't know already, the compiler transforms query syntax into extension method syntax.
- upthedale 14y agoOh yes, I did actually realise that. Thank you though. I worded it badly. I should have been clearer in that I was following on from solutionyogi's argument about readability. The compiler example is a bit on the ugly side, wouldn't you say? To then access 'keywords', it becomes ... .Where(anon => anon.keywords[0] == "verybadexample") .Select(anon => anon.post); What I should have said was that I'm not sure how you would scope things in the same way as the 'let' clause when using extension methods with the same level of readability.
- andy_t 14y agoLike this: var thing = from x in stuff let derp = x.herp select { x.name } Equals this: var thing = stuff.Select( x => { var derp = x.herp; return new { x.name }; } ); edit: formatting. I think each have their place, but this absolutely enrages me: var things = ( from x in thingList select x ).ToList()
- deleted 14y ago[deleted]
- solutionyogi 14y agoYes, Eric Lippert, principal developer of C# compiler, has been drumming for a long time that query should never have a side effect. It should be about filtering and sorting the data. See: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/forea...
- bunderbunder 14y agoRegarding point 1, I would add there are also things that can be done using the query syntax that simply can't with the extension methods. Quite the opposite. Everything in the query syntax gets translated into method calls, even if the way in which it is done so is sometimes non-obvious.[1] However, there are a lot of extension methods that don't have an equivalent in the query syntax - many of the aggregation functions, for example. [1]:http://stackoverflow.com/questions/1092687/code-equivalent-to-the-let-keyword-in-chained-linq-extension-method-calls http://stackoverflow.com/questions/1092687/code-equivalent-t...