ORMLITE - What is the best way to get an IQueryable object ?

1,013 views
Skip to first unread message

jpolvora

unread,
Nov 17, 2011, 10:39:24 AM11/17/11
to ServiceStack .NET Open Source REST Web Services Framework
I'm using the repository pattern with OrmLite. I have a "Get" method
on which I pass a function to filter the query.
Previously, I was using Entity Framework working with IDbSet/
IObjectSet. The query is performed only when iterating the IEnumerable
object, based on a IQueryable filter.

I was looking the extensions methods of OrmLite, and I found the Each
method, returning IEnumerable, then I can cast it to IQueryable
easily.

Is there another way to achieve this without selecting all data from
DB ?

Demis Bellot

unread,
Nov 17, 2011, 11:24:14 AM11/17/11
to servic...@googlegroups.com
Hi Jone,

Firstly there's no IQueryable or Linq in OrmLite it's pretty much just using SQL or filters.
Methods with "Each" in them return back a lazily evaluated list.

Generally you just query by passing in a "where sql", "anon types" or full sql, eg:

var results = dbCmd.Select<Poco>("Name = {0}", name);
var results = dbCmd.Where<Poco>("Name", name);
var results = dbCmd.Where<Poco>(new { Name = name });
var results = dbCmd.Select<Poco>("Select * from Poco Where Name = {0}", name);
var results = dbCmd.Query<Poco>("Select * from Poco Where Name = @name", new { name });

For building of queries the latest version uses a SqlBuilder class which @samsaffron talks about here:

You can return a builder class that other code uses to construct the query and then use the builder output in a new dbCmd query, e.g:

var count = dbCmd.QuerySingle<Poco>(count.RawSql, count.Parameters);
var rows = dbCmd.Query<Poco>(selector.RawSql, selector.Parameters);

Micro ORM's are not for everyone and if you have existing code using IQueryable, it might be better to just leave it as-is.

Cheers,
Reply all
Reply to author
Forward
0 new messages