using块需要如下所示:
using (var unitOfWork = this.unitOfWorkFactory.Create(LockType.Read))
{
var allResult = blogRepository.Retrieve().Join(
userRepository.Retrieve(),
b => b.UserId,
u => u.Id,
(blog, user) => new
{
blog,
user
}
).GroupJoin(
postRepository.Retrieve(),
b => b.blog.Id,
p => p.BlogId,
(anon, posts) => new
{
anon.blog,
anon.user,
posts
}
);
// here the allResult variable needs to get iterated by two selects
// the first select will use automapper to map each entity into another anonymouse type
// the second select will then put the user and posts into the blog and return the blog
return View(allResults);
}
因此,第一个选择将如下所示:
allResults.Select(x => new
{
BlogDto = Mapper.Map<BlogDto>(x.blog),
UserDto = Mapper.Map<UserDto>(x.user),
PostDtos = Mapper.Map<IEnumerable<PostDto>(x.posts)
});
第二个选择将它们合并为一个博客:
allResults.Select(firstSelect).Select(x =>
{
x.BlogDto.User = x.UserDto;
x.BlogDto.Posts = x.PostDtos;
return x.BlogDto;
}
这会给你一个
IEnumerable<BlogDto>
包含视图所需的所有信息。