With Linq you can easily do:
from c in session.Query<Site>()
select new {c.SiteName, c.SiteId,
c.Network, c.RegionLocation.City,
c.RegionLocation.Region.RegionName};
Is there a way to do something like this with QueryOver
? It seems that you can do it with SelectList
but that does not seem as clean as linq approach. Is there a cleaner way ?
No not as far as I know, its a bit more verbose then Linq. But isn't this cleanish?
session.QueryOver<Sites>()
.SelectList(x => x
.Select(p => p.Name)
.Select(p => p.Lat)
.Select(p => p.Lng)
)
.TransformUsing(Transformers.AliasToBean<MarkerDto>())
.List<MarkerDto>();
public class MarkerDto
{
public string Name { get; set; }
public decimal? Lat { get; set; }
public decimal? Lng { get; set; }
}
If your select list is shared across multiple queries you can attempt to stay dry by using a projectionBuilder
eg
Func<QueryOverProjectionBuilder<MarkerDto>,
QueryOverProjectionBuilder<MarkerDto>> GetDtoList() {
MarkerDto dto = null;
return list => list
.Select(w => w.Name).WithAlias(() => dto.Name)
.Select(w => w.Lat).WithAlias(() => dto.Lat)
.Select(w => w.Lng).WithAlias(() => dto.Lng);
}
and use like
Session.QueryOver<Sites>()
.SelectList(GetDtoList())
.TransformUsing(Transformers.AliasToBean<MarkerDto>())
.List<MarkerDto>();
A little bit cleaner but more cumbersome creating a projectionBuilder
, however why change from Linq if it works for you?
I should also mention that it is possible to create a transformer that transforms into an anonymous type, see this blog post , disclaimer mine!
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.