Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack ORMLite - Select columns

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.

like image 952
Alexis Avatar asked Oct 02 '12 18:10

Alexis


1 Answers

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.

like image 122
mythz Avatar answered Sep 21 '22 17:09

mythz