I have an entity named Tag with a navigation property (collection) named Articles. The Tag has a ignored-property named ArticleCount that used to save tha associated Article s count (just in run-time and to use in views- ignored against the DB). See:
public class Tag{
public int Id { get; set; }
public int Title { get; set; }
public virtual ICollection<Article> Articles { get; set; }
[NotMapped]
public int ArticleCount { get; set; }
}
How can I select all Tag s (or one) with it’s ArticleCount in ONE query against the dataBase -in entity framework 4.1 code-first, with lambda expressions please?
(I would design this differently –
ArticleCountshould not be part of the model, or it should be a projection onArticles, which you could eager-load usingInclude(). But this does what you want)Update: my design.
Of course, whether this performs well or not depends on the the expected article count per tag. If it’s a few dozen, this will work reasonably, while being cleaner than the other approach. If it’s hundreds, the other one is better.
Now, if that’s the case, you should use an anonymous or named presentation type instead of reusing the entity.