3

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 ?

1 Answer 1

9

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 e.g.

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Maybe because it is cleansih ... change from Linq to QueryOver ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.