Reputation: 3493
I'm using lastest ServiceStack OrmLite(currently v4.5.6) with C#
I need to return asQueryable from a method, such as;
using (IDbConnection databaseConnection = _databaseFactory.Open())
{
SqlExpression<T> sqlExpression = databaseConnection.From<T>();
IQueryable<T> asQueryable = databaseConnection.LoadSelect(sqlExpression, include)
.AsQueryable();
return asQueryable;
}
But as you can see, loadSelect already going to sql server like;
Select PARAMSetc FROM Table
So I just need IQueryable without going to sql server. I did it with Entity Framework, here is the code;
public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate)
{
return _dbSet.Where(predicate);
}
And yes, I'm writing a kind of generic repository wrapper and I know repository patterns should not return IQueryable because someone use this method and could make performance errors etc. This is out of my topic right now.
How can I return IQueryable with OrmLite ?
Upvotes: 4
Views: 921
Reputation: 143399
OrmLite uses it's own typed API for querying using either simple lambda expressions for simple queries:
var results = db.Select<Poco>(x => x.Id == 1);
Or a Typed SqlExpression<T>
which provides a Typed API modeled closely over SQL which also uses LINQ-like lambda expressions to provide a typed, rich API for querying RDBMS's, e.g:
var q = db.From<Track>()
.Where(x => customYears.Contains(x.Year))
.And(x => x.Name.Contains("A"))
.GroupBy(x => x.Year)
.OrderByDescending("Total")
.ThenBy(x => x.Year)
.Take(2);
var results = db.Select(q);
OrmLite doesn't implement IQueryable<T>
, if you need an SQL IQueryable<T>
implementation you'll need to use an ORM that implements it like EF.
The only way to get an IQueryable<T>
in any other ORM is to return it convert from a .NET List<T>
that OrmLite returns, e.g:
List<Poco> results = db.Select<Poco>();
IQueryable<Poco> queryable = results.AsQueryable();
Upvotes: 2
Reputation: 859
I saw a post like this; here
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: http://samsaffron.com/archive/2011/09/05/Digging+ourselves+out+of+the+mess+Linq-2-SQL+created
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, example:
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.
Upvotes: 1