我正在尝试将实体的集合投影到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;}
}
Run Code Online (Sandbox Code Playgroud)
查询有点像:
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>();
Run Code Online (Sandbox Code Playgroud)
编辑更新
即使没有变换,我也似乎无法运行简单的投影并得到:"索引超出了数组的范围":
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>();
Run Code Online (Sandbox Code Playgroud)
我猜这是你应该做的..
将您的DTO更新为:
public class MyDTO
{
public string BlogName {get;set;}
public IList<Comments> Comments {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
您修改后的查询:
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),
.TransformUsing(Transformers.AliasToBean<MyDTO>()
.SingleOrDefault<MyDTO>()
Run Code Online (Sandbox Code Playgroud)
如果那不起作用那么
_session.QueryOver<Blog>(() => blogAlias)
.JoinAlias(x => x.Comments, () => commentsAlias, JoinType.LeftOuterJoin)
.Select(Projections.Property(() => blogAlias.Reference),
Projections.Property(() => blogAlias.Comments))
.SingleOrDefault<object[]>()
.Select(x=>new MyDTO {BlogName=(string)x[0],Comments=x[1].Select(y=>y.ToString()).ToList())};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8473 次 |
| 最近记录: |