简体   繁体   中英

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.

You can use OrmLite's SelectMulti API for this, eg:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM