pythonlinqlinq-to-sqlironpythonboo

Can you do LINQ-like queries in a language like Python or Boo?


Take this simple C# LINQ query, and imagine that db.Numbers is an SQL table with one column Number:

var result = 
    from n in db.Numbers
        where n.Number < 5
        select n.Number;

This will run very efficiently in C#, because it generates an SQL query something like

select Number from Numbers where Number < 5 

What it doesn't do is select all the numbers from the database, and then filter them in C#, as it might appear to do at first.

Python supports a similar syntax:

result = [n.Number for n in Numbers if n.Number < 5]

But it the if clause here does the filtering on the client side, rather than the server side, which is much less efficient.

Is there something as efficient as LINQ in Python? (I'm currently evaluating Python vs. IronPython vs. Boo, so an answer that works in any of those languages is fine.)


Solution

  • I believe that when IronPython 2.0 is complete, it will have LINQ support (see this thread for some example discussion). Right now you should be able to write something like:

    Queryable.Select(Queryable.Where(someInputSequence, somePredicate), someFuncThatReturnsTheSequenceElement) 
    

    Something better might have made it into IronPython 2.0b4 - there's a lot of current discussion about how naming conflicts were handled.