SqlExpression and references

Using an expression that is built up and then a select performed using that expression, is it possible to have references automatically loaded?

Psuedo code of what currently works, say for example that Foo contains a collection property that has ReferenceAttribute:

var exp = Db.From<Foo>();
exp = exp.Where( f => f.Condition == true );
var results = Db.Select( exp );
foreach( var fooInst in results )
    Db.LoadReferences( fooInst );

It’d be nice to eliminate the step of iterating over the results collection.

No, OrmLite’s References only supports 1 level deep to avoid any N+1 queries. It also uses clean, disconnected POCOs where lazily loading references isn’t an option either.

If your references are only 1-level deep you can just use LoadSelect() to load its references, e.g:

var results = Db.LoadSelect(exp);

Otherwise your sample code is an example of N+1 query which OrmLite doesn’t include in any of its APIs. You can wrap that behind your own custom generic extension method, e.g:

var results = Db.Select(q).LoadAllReferences();

But N+1 APIs like this isn’t something we’d want in OrmLite as devs should explicitly be aware which code makes any N+1 queries, which ideally should be avoided.

Thank you for the response mythz.

One level deep is all that is required at this time so LoadSelect() will be sufficient.

1 Like