Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 229
apenwarr Avatar asked Nov 30 '22 07:11

apenwarr


1 Answers

sqlsoup in sqlalchemy gives you the quickest solution in python I think if you want a clear(ish) one liner . Look at the page to see.

It should be something like...

result = [n.Number for n in db.Numbers.filter(db.Numbers.Number < 5).all()]
like image 169
David Raznick Avatar answered Dec 10 '22 03:12

David Raznick