Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servicestack ORMLite Query Multiple

I was wondering if ORMLite had a QueryMultiple solution like dapper.

My use case is in getting paged results.

return new {
  Posts = conn.Select<Post>(q => q.Where(p => p.Tag == "Chris").Limit(20, 10))
  TotalPosts = conn.Count<Post>(q.Where(p => p.Tag == "Chris"))
};

I also have a few other cases where I'm calculating some other stats in addition to a main query, and I'm keen to avoid multiple roundtrips.

(Probably unrelated, but I'm using PostgreSQL)

like image 303
Chris Avatar asked May 09 '13 15:05

Chris


People also ask

Can I do a simple SQL join in servicestack ormlite?

I'd like to do a simple SQL join in ServiceStack OrmLite and get both tables as the corresponding .NET objects. This would give me an anonymous type with Claim and Policy properties. I've looked at the OrmLite Advanced Join Example, but that only selects some of the properties of each type into a 3rd type of object ( FullCustomerInfo ).

How can I use ormlite to create a typed query?

An alternative is to use a combination of OrmLite to create the typed query using its built-in Reference Conventions and then use OrmLite's embedded version of Dapper to Query Multiple result-sets into your existing POCO Types. To start with create your Typed Query Expression and have it return all fields from all tables with:

Does ormlite support collection of multiple values in SQL?

City, cities )); OrmLite also supports providing collection of values which is automatically split into multiple DB parameters to simplify executing parameterized SQL with multiple IN Values, e.g: var ids = new [] { 1, 2, 3 }; var results = db.

How do I use ormlite's advanced join example?

So to use OrmLite's Advanced Join Example you can construct a typed Join Query with: Then use the SelectMulti APIs to populate the tables you're interested in, e.g: Which will return a List<Tuple<T,T2,T3>> giving you typed access to your populated POCOs from tables in your joined query.


1 Answers

You can probably do something like this:

var bothThings = db.Exec(cmd => {

    cmd.CommandText = @"
        select * from TableA
        select * from TableB";

    var both = new BothAandB();

    using (var reader = cmd.ExecuteReader())
    {
        both.a = reader.ConvertToList<A>();
        reader.NextResult();
        both.b = reader.ConvertToList<B>();
    }

    return both;

});

It might be possible to wrap this up in an extension method, but nothing clever is coming to mind.

like image 129
Master Morality Avatar answered Sep 22 '22 14:09

Master Morality