My DTO:
public class ServiceWithCount
{
public Service Service { get; set; }
public long Count { get; set; }
}
and:
public class Service
{
public virtual long Id { get; set; }
...
}
public class Vote
{
public virtual long Id { get; set; }
public virtual Service Service { get; set; }
...
}
I would like to do something like:
ServiceWithCount dto = null;
Service serviceAlias = null;
var result = session.QueryOver<Service>(() => serviceAlias)
.Where(x => x.Serie.Id == serie.Id)
.SelectList(list =>
list
.SelectSubQuery(QueryOver.Of<Vote>().Where(y => y.Service.Id == serviceAlias.Id).ToRowCountInt64Query()).WithAlias(() => dto.Count)
.Select(x => x).WithAlias(() => dto.Service) //This line causes error
)
.TransformUsing(Transformers.AliasToBean<ServiceWithCount>())
.List<ServiceWithCount>();
but this causes an error:
could not resolve property: of: MyNamespace.Service
How to return root type object as component of dto?
A solution using Linq:
add the namespace:
using System.Linq;
using NHibernate;
using NHibernate.Linq;
And query something like this:
var result = (from s in session.Query<Service>()
where s.Serie.Id == serie.Id
let count = session.Query<Vote>().Count(v => v.Service.Id == s.Id)
select new ServiceWithCount() { Service = s, Count = count })
.ToList();
I think exposing your entity Service
in the DTO is not a good pratice, you could expose simple properties just to get the information you want to transfer, avoiding the risk to get a proxy from nhibernate inside the DTO.