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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:07:47+00:00 2026-05-11T20:07:47+00:00

I have some Linq code and it’s working fine. It’s a query that has

  • 0

I have some Linq code and it’s working fine. It’s a query that has a subquery in the Where clause. This subquery is doing a groupby. Works great.

The problem is that I don’t know how to grab one of the results from the subquery out of the subquery into the parent.

Frst, here’s the code. After that, I’ll expplain what piece of data i’m wanting to extract.

    var results = (from a in db.tblProducts
               where (from r in db.tblReviews
                      where r.IdUserModified == 1
                      group r by
                          new
                              {
                                  r.tblAddress.IdProductCode_Alpha,
                                  r.tblAddress.IdProductCode_Beta,
                                  r.tblAddress.IdProductCode_Gamma
                              }
                      into productGroup
                          orderby productGroup.Count() descending
                          select
                          new
                              {
                                  productGroup.Key.IdProductCode_Alpha,
                                  productGroup.Key.IdProductCode_Beta,
                                  productGroup.Key.IdProductCode_Gamma,
                                  ReviewCount = productGroup.Count()
                              }).Take(3)
                   .Any(
                   r =>
                   r.IdProductCode_Alpha== a.IdProductCode_Alpha&& 
                       r.IdProductCode_Beta== a.IdProductCode_Beta&&
                       r.IdProductCode_Gamma== a.IdProductCode_Gamma)
               where a.ProductFirstName == ""
               select new {a.IdProduct, a.FullName}).ToList();

Ok. I’ve changed some field and tables names to protect the innocent. 🙂

See this last line :-

select new {a.IdProduct, a.FullName}).ToList();

I wish to include in that the ReviewCount (from the subquery). I’m jus not sure how.

To help understand the problem, this is what the data looks like.

Sub Query

IdProductCode_Alpha = 1, IdProductCode_Beta = 2, IdProductCode_Gamma = 3, ReviewCount = 10
… row 2 …
… row 3 …

Parent Query

IdProduct = 69, FullName = ‘Jon Skeet’s Wonder Balm’

So the subquery grabs the actual data i need. The parent query determines the correct product, based on the subquery filters.

EDIT 1: Schema

tblProducts

  • IdProductCode
  • FullName
  • ProductFirstName

tblReviews (each product has zero to many reviews)

  • IdProduct
  • IdProductCode_Alpha (can be null)
  • IdProductCode_Beta (can be null)
  • IdProductCode_Gamma (can be null)
  • IdPerson

So i’m trying to find the top 3 products a person has done reviews on.

The linq works perfectly… except i just don’t know how to include the COUNT in the parent query (ie. pull that result from the subquery).

Cheers 🙂

  • 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-11T20:07:47+00:00Added an answer on May 11, 2026 at 8:07 pm

    Got it myself. Take note of the double from at the start of the query, then the Any() being replaced by a Where() clause.

    var results = (from a in db.tblProducts
                   from g in (
                      from r in db.tblReviews
                      where r.IdUserModified == 1
                      group r by
                          new
                              {
                                  r.tblAddress.IdProductCode_Alpha,
                                  r.tblAddress.IdProductCode_Beta,
                                  r.tblAddress.IdProductCode_Gamma
                              }
                      into productGroup
                          orderby productGroup.Count() descending
                          select
                          new
                              {
                                  productGroup.Key.IdProductCode_Alpha,
                                  productGroup.Key.IdProductCode_Beta,
                                  productGroup.Key.IdProductCode_Gamma,
                                  ReviewCount = productGroup.Count()
                              })
                      .Take(3)
               Where(g.IdProductCode_Alpha== a.IdProductCode_Alpha&& 
                   g.IdProductCode_Beta== a.IdProductCode_Beta&&
                   g.IdProductCode_Gamma== a.IdProductCode_Gamma)
               where a.ProductFirstName == ""
               select new {a.IdProduct, a.FullName, g.ReviewCount}).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I frequently use the rel (and rev) attributes with a… May 11, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer The php client doesn't handle persistent connections. you either need… May 11, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer You can run PowerShell on Linux via Pash. It uses… May 11, 2026 at 9:12 pm

Related Questions

I've been spending some time refactoring my C# code, and I'm struck by how
i have the an IList and i wish to turn it into a ToArray()
If I have the following Linq code: context.Table1s.InsertOnSubmit(t); context.Table1s.InsertOnSubmit(t2); context.Table1s.InsertOnSubmit(t3); context.SubmitChanges(); And I get
I'm new to using Linq and just started a side project to learn some
Before I start, I know there is this post and it doesn't answer my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.