Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:59:49+00:00 2026-06-15T21:59:49+00:00

I am looking to determine the cleanest / best practice way to complete the

  • 0

I am looking to determine the cleanest / best practice way to complete the following:

I am using EF, code first, repository pattern. I have an Entity called Project which in turn has a list of Tags or Keywords which describe the project:

public class Tag {
   public int TagID { get; set; }
   public int ProjectID { get; set; }
   public string TagValue { get; set; }
}  

I am using this data in a strongly typed Razor view, and am displaying the Tags below the Project title. When doing this I want each tag to indicate how many times it appears in the data base like:

Some Project Title

C# (5), Entity Framework (17)

It seemed to me the best plan would be to add a calculated property to the entity:

public class Tag {
   public int TagID { get; set; }
   public int ProjectID { get; set; }
   public string TagValue { get; set; }
   public int TagCount { get { _context.Tags("Some Filter on TagValue").Count() }}
}

Alternately, I was considering populating the TagCount property in the controller calling a method in the TagRepository like:

public int countTags(string TagValue);

Realizing I may be missing the bigger picture here altogether, my question is: Are either of these approaches on the right track?

Update: Adding the Project model as requested.

Abstract Project:

public abstract class Project {
    public int ProjectID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

Implemented Type of Project:

public class Scientific : Project {
    public int ScientificProjectNumber { get; set; }
    public string FileNumber { get; set; }
    public int Year { get; set; }
    public DateTime StartDate { get; set; }

}

UPDATE:
My solution based on Brian Cauthon’s answer was to do the following:

-Instead of creating a net-new class, I inherited the ones I was extending:
Note: I had to create an additional property TagsView as I could not override the virtual property on the Scientific : Project class.

public class ScientificView : Scientific {
    public virtual ICollection<TagView> TagsView { get; set; }
}

-I did the same for TagView, extending the class rather than copying it – hopefully saving me some effort as these object evolve:

    public class TagView : Tag {
    public int TagCount { get; set; }
}

-Added the suggested TagRepository Method:

public Dictionary<string, int> countTags(IEnumerable<string> tags) {
        Dictionary<string, int> result = new Dictionary<string, int>();
        var query = from o in _context.Tags
                group o by o.TagValue into tagGroup
                select new {TagText = tagGroup.Key,
                    TagCount = tagGroup.Count()};

        foreach (var tagGroup in query) {
            result.Add(tagGroup.TagText, tagGroup.TagCount);
        }
        return result;
    }

-Updated executed all from the Controller. Thank you for the link to the excellent AutoMapper project:

public ViewResult Scientific(int projectID) {
        Scientific project= _scientificRepository.Scientific.FirstOrDefault(l => l.ProjectID == projectID);
        Dictionary<string, int> tagCounts = _tagRepository.countTags(project.Tags.Select(t => t.TagValue));

        Mapper.CreateMap<Scientific, ScientificView>();
        ScientificView sv = Mapper.Map<Scientific, ScientificView>(project);

        Mapper.CreateMap<Tag, TagView>();
        sv.TagsView = new List<TagView>();
        foreach (Tag t in project.Tags) {
            TagView tv = Mapper.Map<Tag, TagView>(t);
            tv.TagCount = tagCounts[tv.TagValue];
            sv.TagsView.Add(tv);
        }
        return View(sv);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:59:50+00:00Added an answer on June 15, 2026 at 9:59 pm

    I would go with pulling the count from the tag repository. Since you are likely to be pulling counts for multiple tags at a time I would change your repository method to pull all at once.

    I would create specific viewmodels for your view and then map your Project and Tag to them with Automapper.

    public class ProjectView {
        public int ProjectID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public virtual ICollection<TagView> Tags { get; set; }
    }
    
    public class ProjectTagView {
       public int TagID { get; set; }
       public string TagValue { get; set; }
       public int TagCount { get; set; }
    }
    

    Then in your controller you would have something like this.

    var model = AutoMapper.Map<Project,ProjectView>(project);
    Dictionary<string,int> tagCounts = tagRepository.getCounts(model.Tags.Select(t=>t.TagValue));
    foreach(var t in model.Tags){
        t.TagCount = tagCounts[t.TagValue];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for the best way to determine if a URI exists in VB.NET without
I'm looking for a way to determine the code to text ratio of a
I'm looking for a way to reliably determine whether C++ code is being compiled
I am looking for a way to determine for a thread on which other
I am looking for a way to quickly determine if a PNG image has
I'm looking for a way to determine the difference between two dates. A normal
I'm looking for a way to use your location to determine whether you need
So I am a little confused, I have been looking around trying to determine
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking to do a bit of refactoring... Using NHibernate I have this query currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.