ServiceStack ORMLite - Select columns

Alexis picture Alexis · Oct 2, 2012 · Viewed 7.2k times · Source

I recently started working with ServiceStack and its ORMLite framework. I have searched on Google and browsed the source code but couldn't find anything relevent.

Is there any way to select specific columns when executing a query with ORMLite ? Something like that : Db.First<Model>(q => q.Id == someId, "Column1, Column2")

Unless I missed this feature, I am surprised nobody asked about this before, since this is one the rule of thumbs to optimize your DB transactions.

Answer

mythz picture mythz · Oct 2, 2012

If you want to specify columns other that the table you need to use SQL as seen in this earlier example

So in your case you could do something like:

Db.First<Model>("SELECT Column1, Column2 FROM AnyTableOrView");

You can also create a partial model that looks at your table by decorating it with the [Alias] attribute, like:

[Alias("AnyTableOrView")]
public class Model {
    public int Id { get; set; }
    public string Column1 { get; set; }
    public string Column2 { get; set; }
}

Then you can do something like:

Db.First<Model>(q => q.Id == someId);

And it will only SELECT + populate fields from the partial model.