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)
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 ).
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With