In other programming languages, I have seen Map and Reduce, and those are cornerstones of functional programming. I could not find any reasoning or history why LINQ has Aggregate
(same as Reduce
) and Select
(same as Map
)?
Why I am asking is that it took me a while to understand it is the same thing and I am curious what is the reasoning for this.
Best Answer
This mostly comes down to the history of LINQ.
LINQ was originally intended to be SQL-like, and used (largely, though not exclusively) to connect to SQL databases. This leads to much of its terminology being based on SQL.
So, "select" came from the SQL
select
statement, and "aggregate" came from SQL aggregate functions (e.g.,count
,sum
,avg
,min
,max
).For those who question the degree to which LINQ originally related to SQL, I'd refer to (for example) Microsoft's articles on Cω, which was a language devised by Microsoft Research, and appears to be where most of the basics of LINQ were worked out before they were added to C# and .NET.
For example, consider an MSDN article on Cω, which says:
At least as far as I know, the XPath-based operators were never added to C#, leaving only the operators that were documented (before LINQ existed) as being based directly on SQL.
Now, it's certainly true that LINQ isn't identical to the SQL-based query operators in Cω. In particular, LINQ follows C#'s basic objects and function calls syntax much more closely than Cω did. Cω queries followed SQL syntax even more closely, so you could write something like this (again, drawn directly from the article linked above):
And yes, the same article does talk specifically about using the SQL-based queries to query data coming from actual SQL databases:
So, yes, from the very beginning (or even before the beginning, depending on your viewpoint) LINQ was explicitly based on SQL, and intended specifically to allow access to data in SQL databases.