我有一個實體:如何通過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