[IronPython] Extension methods...

Keith J. Farmer kfarmer at thuban.org
Tue Sep 20 18:38:21 CEST 2005

LINQ is *very* simple.  It's simply, as Anders puts it, a pattern for
describing queries.  It's made useful by way of extension methods and
the compiler deciding to convert a lambda expression not into a
delegate, but into an expression tree.
The query methods (Where, OrderBy, Select, etc) take trees, and build up
a description of the query which is then evaluated only when enumerated.
This allows DLINQ, for example, to analyze the tree and produce a SQL
command to send to a database, or XLINQ to produce an XQuery expression,
or LINQ to just take it as-is.  If you'd used a list comprehension,
you'd end up pulling all the information down from the database (were
this a database table); since this is an expression that can be
analyzed, you can manipulate it before it's actually invoked.  
You can conceivably take the expression and ship it whole to some
service, for the service to deal with.  Were this a CLR host (such as
Yukon), you'd have, effectively, created a remoted procedure.
Thus, it's *not* the same as a Python comprehension.  The syntax could
be similar, but the compiler would decide what to do based on whether
the method in question takes a Func<> or an Expression<>.
So I would say you were close, but not quite accurate.  Take a few
passes through the spec -- it's much more interesting than it seems at
Keith J. Farmer // kfarmer at thuban.org

-----Original Message-----
From: users-ironpython.com-bounces at lists.ironpython.com
[mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of
Sent: Tuesday, 20 September 2005 08:23

Here's the Python equivalent:

>>> numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
>>> lownums = [n for n in numbers if n < 5]
>>> lownums
[4, 1, 3, 2, 0]

For most simple extractions of this sort, I prefer the Python list
comprehension.  IMO, the power of LINQ comes from integrating to
using the same syntax as you would for other IEnumerable objects.

More information about the Ironpython-users mailing list