我正在尝试将实体的集合投影到 DTO 中。简单的属性很容易,但集合有问题:
public class Blog
{
public string Name {get;set;}
public IList<Comments> Comments {get;set;}
//... more properties
}
public class Comments
{
public Blog Blog {get;set;}
//... more properties
}
public class MyDTO
{
public string BlogName {get;set;}
public IList<Comments> {get;set;}
}
查询有点像:
var dto = _session.QueryOver<Blog>(() => blogAlias)
.JoinAlias(x => x.Comments, () => commentsAlias, JoinType.LeftOuterJoin)
.Select(
Projections.Property(() => blogAlias.Reference).WithAlias(() => myDTO.Reference),
// what project here to project blogAlias.Comments into myDTO.Comments))
.TransformUsing(Transformers.AliasToBean<MyDTO>()
.SingleOrDefault<MyDTO>();
编辑更新
即使没有转换,我似乎也无法运行简单的投影并得到:“索引超出了数组的范围”:
var dto = _session.QueryOver<Blog>(() => blogAlias)
.JoinAlias(x => x.Comments, () => commentsAlias, JoinType.LeftOuterJoin)
.Select(
Projections.Property(() => blogAlias.Reference).WithAlias(() => myDTO.Reference),
Projections.Property(() => blogAlias.Comments).WithAlias(() => myDTO.Comments)
.List<object>();