comment mapper un objet anonyme à une classe par AutoMapper?

J'ai une entité:

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; }
}

et un modèle:

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; }
}

et j'ai une requête de l'entité comme ceci (par EF ou NH):

var tagsAnon = _context.Tags
    .Select(t => new { Tag = t, PostsCount = t. Posts.Count() })
    .ToList();

Maintenant, comment puis-je carte la tagsAnon (comme un objet anonyme) à une collection de TagModel (par exemple ICollection<TagModel> ou IEnumerable<TagModel>)? Est-il possible?

  • Pourquoi ne pas la carte Tag directement à TagModel? Pourquoi les objets intermédiaires?
InformationsquelleAutor agent47 | 2012-03-09