ORMLite/Servicestack: Joining Multiple Nested Tables Together
I think this should be easy, and I'm not sure if I'm just missing something or if this feature is missing from ServiceStack/ORMLite currently. I've got a tablestructure that looks something like this:
[Table Foo]
Id: GUID
... Fields
[Table Bar]
Id: GUID
FooId: GUID (references Foo.Id)
... Fields
[Table Baz]
Id: GUID
BarId: GUID (References Bar.Id)
... Fields
And what I want to do is return a single object encapsulating all of Foo, Bar and Baz together in a single return, so I can be efficient with my DB requests. Something like:
SELECT *
FROM foo, bar, baz
WHERE foo.Id = bar.FooId && bar.Id = baz.BarId
I know how I want to write it in SQL, but I'm struggling with how to write it in ORMLite/Servicestack. This answer from Mythz seems to point to this not being a thing in ServiceStack yet but that answer was also literally years ago so I'm wondering what if anything has changed in the meantime. I could iterate through the objects and load up that second join manually, since I really only need a single field from that last nested table, but that still feels like doing too many calls to the DB when really I SHOULD be able to do this all in a single call, but I can't find any documentation on how.
你可以使用OrmLite的SelectMulti API,例如:
var q = db.From<Foo>()
.Join<Foo, Bar>()
.Join<Bar, Baz>();
var results = db.SelectMulti<Foo, Bar, Baz>(q);
foreach (var tuple in results)
{
Foo foo = tuple.Item1;
Bar bar = tuple.Item2;
Baz baz = tuple.Item3;
}
链接地址: http://www.djcxy.com/p/65982.html