3 ms·
I vaguely remember LINQ from when I had a real job, but this is way cooler. I just remembered it as chained function calls on a database query return (you could
by tehwalrus 13y ago
I vaguely remember LINQ from when I had a real job, but this is way cooler. I just remembered it as chained function calls on a database query return (you could do your filtering in C# instead of in the SQL) but this just looks like SQL (or better - python) in C#.
- lucian1900 13y agoThe syntax in the article desugars to the method calls you remember (which are the common HOFs). Since the calls are lazy, they can also generate SQL when asked to execute.
- dashN9ne 13y agoI've not heard the term 'HOF' before and didn't find it/expand it with a cursory search. Could you define/expand it?
- lucian1900 13y agoI had (mistakenly) thought it's a commonly known term. Wikipedia explains it better than I could http://en.wikipedia.org/wiki/Higher-order_function http://en.wikipedia.org/wiki/Higher-order_function
- WickyNilliams 13y agoLINQ comes in many flavours e.g. LINQ to XML, LINQ to Objects. You're talking about LINQ to SQL. The chained function calls are equivalent to the more SQL-style LINQ, it's a matter of personal preference and which sits better given the context. Personally I always prefer the chained methods with lambdas. However don't let the seemingly chained methods fool you! The great thing about LINQ is that is defers execution, by building an expression tree [1], meaning the code is lazily executed only when needed. And the expression tree means that everything (filtering, projection etc) is done in a single for loop [2]. e.g. var results = collection.Select(item => item.Foo).Where(foo => foo < 3); This builds an expression tree, but no work has been undertaken on our collection. When we attempt to enumerate the results, the expression tree is converted to code (again, as a single for loop, not multiple iterations as the code might seem): results.ToList(); It's extremely cool to work with and highly expressive. One of my favourite features of C# [1] http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-... [2] http://stackoverflow.com/questions/7324033/what-are-the-benefits-of-a-deferred-execution-in-linq#answer-7324095 http://stackoverflow.com/questions/7324033/what-are-the-bene...