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 962891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:34:56+00:00 2026-05-16T01:34:56+00:00

I want to group by the categoryid and then do a count on this.

  • 0

I want to group by the categoryid and then do a count on this. But I don’t know how to do this. I have tried a couple of ways without success. Here is my latest:

public class Count
{
    public int TradersCount { get; set; }
    public int Id { get; set; }
    public string Description { get; set; }       
}


    public IQueryable<Count> CountTradersAttachedToCategories()
    {
        var data = from tc in _db.tblTradersCategories
                   select new Count
                  {

                        Description = tc.tblCategory.description,
                        Id = tc.tblCategory.categoryId,
                        TradersCount = tc.Select(x => x.categoryid).GroupBy().Count()

                   };

        return data;
    }
  • tblTradersCategories joins both
    tblTraders/tblCategories
  • A single trader can have many categories
  • A single category can have many traders

Thanks in advance for any help.

Clare

  • 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-16T01:34:57+00:00Added an answer on May 16, 2026 at 1:34 am

    Try this:

    var data = from tc in _db.tblTradersCategories
               group tc by new { tc.tblCategory.categoryId,
                                 tc.tblCategory.description } into g
               select new { Count = g.Count(),
                            Id = g.Key.categoryId,
                            Description = g.Key.description };
    

    If you want that in your Count class you may need to use AsEnumerable() to perform the conversion in process:

    var converted = data.AsEnumerable()
                        .Select(c => new Count { TradersCount = c.Count,
                                                 Id = c.Id,
                                                 Description = c.Description });
    

    You can try doing them all in one go:

    var data = from tc in _db.tblTradersCategories
               group tc by new { tc.tblCategory.categoryId,
                                 tc.tblCategory.description } into g
               select new Count { TradersCount = g.Count,()
                                  Id = g.Key.categoryId,
                                  Description = g.Key.description };
    

    But I don’t know if that will work. It depends on how the LINQ provider handles it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this scenario. I want group on Country and Category. A store can
I want to group RadioButtons, but I don't want to use the visible control
I know there are a couple of questions regarding this Access Error message, but
I want to group results of query by the item id and count number
I have one folder with about 1000 files and I want to group them
I want to group subcateogries within categories. The subcategories can have any number of
I got 3 tables. I want to group by the item.name and count the
I want to use a pie chart per group in Jasper iReport, but I
I have this schema: Products (**ProductId**, OwnerId, Name) Categories (**CategoryId**, Name) ProductsInCategories( **ProductId**, CategoryId
I have an Items table that contains a CategoryId field. This is an FK

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.