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

  • Home
  • SEARCH
  • 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 4340096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:18:12+00:00 2026-05-21T11:18:12+00:00

Say I had a class: public class Post { public int PostId { get;

  • 0

Say I had a class:

public class Post
{
    public int PostId { get; set; }
    public string Topic { get; set; }
    public int UserId { get; set; }
    [StringLength(5000)]
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }
    public string Name { get; set; }
    public int Votes { get; set; }
} 

And for each post, a user could input a topic. for example, if the topics were “Red” “Green” “Blue” and “Yellow”, how could I create a list based on how many times those were used?

An example output:

Red   | 70
Blue  | 60
Green | 40
Yellow| 35

EDIT: How come this doesn’t work and gives me an error where I cannot implicitly convert the type?

public List<string> GetPopularTopics(int count)
    {
        var posts = from p in db.Posts
                    group p by p.Topic into myGroup
                    select new
                    {
                        Topic = myGroup.Key,
                        Count = myGroup.Count()
                    };
        return posts.ToList();
    }

EDIT 2:

So I tried your solution out Dustin, and I’m getting an error. This is what I used:

public IEnumerable<IGrouping<string,int>> GetPosts()
    {
        var posts = from p in db.Posts
                    group p by p.Topic into topicCounts
                    select new
                    {
                        Topic = topicCounts.Key,
                        Count = topicCounts.Count()
                    };
        return posts.ToList();
    }

This is giving me an error under posts.ToList():
Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.Generic.IEnumerable>’. An explicit conversion exists (are you missing a cast?)

  • 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-05-21T11:18:13+00:00Added an answer on May 21, 2026 at 11:18 am

    You must create a new type if you do a projection and return it form method!

    public class MyCounts
    {
        public string Topic { get; set; }
        public int Count { get; set; }
    }
    
    
    public List<MyCounts> GetPopularTopics(int count)
    {
        var posts = from p in db.Posts
                    group p by p.Topic into myGroup
                    select new MyCounts
                    {
                        Topic = myGroup.Key,
                        Count = myGroup.Count()
                    };
        return posts.ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have the following class : class Abc { int id; public: int
I had a class hierarchy setup like so: public abstract class GameController public abstract
Say I had the following code: % Cellmode_subfunction_test.m %% Cell 1 foo(1); %% Cell
Let's say I had a program in C# that did something computationally expensive, like
Say if I had a table of books in a MySQL database and I
How do you organize your Extension Methods? Say if I had extensions for the
Yesterday I had a team leader of another team say that they took a
I've had this problem a couple of times. Let's say I want to display
Say I am calling a third-party API which returns a Post, and I want
Lets say we have a program which contains such classes: public interface AbstractItem {

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.