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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:49:38+00:00 2026-06-18T02:49:38+00:00

Building a bunch of reports, have to do the same thing over and over

  • 0

Building a bunch of reports, have to do the same thing over and over with different fields

    public List<ReportSummary> ListProducer()
    {
        return (from p in Context.stdReports                    
                group p by new { p.txt_company, p.int_agencyId }
                    into g
                    select new ReportSummary
                    {
                        PKi = g.Key.int_agencyId,
                        Name = g.Key.txt_company,
                        Sum = g.Sum(foo => foo.lng_premium),
                        Count = g.Count()
                    }).OrderBy(q => q.Name).ToList();
    }

    public List<ReportSummary> ListCarrier()
    {
        return (from p in Context.stdReports
                group p by new { p.txt_carrier, p.int_carrierId }
                    into g
                    select new ReportSummary
                    {
                        PKi = g.Key.int_carrierId,
                        Name = g.Key.txt_carrier,
                        Sum = g.Sum(foo => foo.lng_premium),
                        Count = g.Count()
                    }).OrderBy(q => q.Name).ToList();
    }

My Mind is drawing a blank on how i might be able to bring these two together.

  • 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-18T02:49:39+00:00Added an answer on June 18, 2026 at 2:49 am

    Since all that changes between the two queries is the group key, parameterize it. Since it’s a composite key (has more than one value within), you’ll need to create a simple class which can hold those values (with generic names).

    In this case, to parameterize it, make the key selector a parameter to your function. It would have to be an expression and the method syntax to get this to work. You could then generalize it into a function:

    public class GroupKey
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    private IQueryable<ReportSummary> GetReport(
        Expression<Func<stdReport, GroupKey>> groupKeySelector)
    {
        return Context.stdReports
            .GroupBy(groupKeySelector)
            .Select(g => new ReportSummary
            {
                PKi = g.Key.Id,
                Name = g.Key.Name,
                Sum = g.Sum(report => report.lng_premium),
                Count = g.Count(),
            })
            .OrderBy(summary => summary.Name);
    }
    

    Then just make use of this function in your queries using the appropriate key selectors.

    public List<ReportSummary> ListProducer()
    {
        return GetReport(r =>
            new GroupKey
            {
                Id = r.int_agencyId,
                Name = r.txt_company,
            })
            .ToList();
    }
    
    public List<ReportSummary> ListCarrier()
    {
        return GetReport(r =>
            new GroupKey
            {
                Id = r.int_carrierId,
                Name = r.txt_carrier,
            })
            .ToList();
    }
    

    I don’t know what types you have mapped for your entities so I made some assumptions. Use whatever is appropriate in your case.

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

Sidebar

Related Questions

I'm building a bunch of forms that have labels and corresponding fields (input element
I am building a whole bunch of different controls from a database schema. When
I'm building an app and currently I have a bunch of links that will
We are building a Java app that creates reports from various data sources. For
I'm building a bunch of windows libraries (mostly simple wrappers to compine features from
I am building a bunch of list items in an un-ordered list. The list
I'm building an app that randomly chooses and displays two images from bunch of
I am Building a learning application where there are a bunch of different page
I'm building a console application that have to process a bunch of document. To
I'm building an application where every user will have a bunch of items. When

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.