我有一个实体:如何通过AutoMapper将匿名对象映射到类?
public class Tag {
public int Id { get; set; }
public string Word { get; set; }
// other properties...
// and a collection of blogposts:
public ICollection<Post> Posts { get; set; }
}
和模型:
public class TagModel {
public int Id { get; set; }
public string Word { get; set; }
// other properties...
// and a collection of blogposts:
public int PostsCount { get; set; }
}
和我查询这样的实体(由EF或NH):
var tagsAnon = _context.Tags
.Select(t => new { Tag = t, PostsCount = t. Posts.Count() })
.ToList();
现在,我该如何映射tagsAnon
(作为ano nymous object)集合到TagModel
(例如, ICollection<TagModel>
或IEnumerable<TagModel>
)?可能吗?
为什么不直接映射'Tag'到'TagModel'?为什么中间对象? – 2012-03-11 01:41:14