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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:36:25+00:00 2026-05-28T13:36:25+00:00

EDIT: Based on one of the posts below, i figured out how to write

  • 0

EDIT: Based on one of the posts below, i figured out how to write it. Answer is at the end of this post.

I have a store… and one of the dropdowns allow you to sort the products by popularity (items that are most purchased, show up first).

I know how to write this in SQL, but i’m failing miserably in LINQ. Can someone translate this for me? Hopefully if I see enough of these examples, i’ll get better at doing it myself…

I simply want to bring back all the items, but in order of popularity. You can tell which are most popular by seeing how many times the itemId comes up in the “OrderDetails” table.

select i.*
from items i
left outer join (
    select od.itemid, ct = COUNT(1)
    from orderdetails od
        join orders o on od.orderid = o.orderid
    where o.ordersubmitteddate is not null
    group by od.itemid
    ) pop on pop.itemid = i.itemid
order by pop.ct desc, i.name

—— ANSWER BELOW ——-

Here’s the answer, I write one query first to determine which “items” i want to show… and then I write a second query to order it….

var items = db.Items.Where("STUFF");

items = from i in items
        join pop in (
            from od in db.OrderDetails
            where od.Order.OrderSubmittedDate != null
            group od by od.ItemId into g
            select new { ItemId = g.Key, Ct = g.Count() }
        ) on i.ItemId equals pop.ItemId into pop_join
        from x in pop_join.DefaultIfEmpty()
        orderby x.Ct descending, i.Name
        select i;
  • 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-28T13:36:26+00:00Added an answer on May 28, 2026 at 1:36 pm

    Well, you could try

    var pop =   from od in context.OrderDetail
                join o in context.Order on od.OrderId equals o.OrderId
                where o.OrderSubmittedDate != null
                group by od.ItemId into g
                select new { ItemId = g.Key, Count = g.Count() };
    
    var query = from p in pop
                join i in items on p.ItemId equals i.ItemId into j
                from x in j.DefaultIfEmpty()
                order by x.Count descending, i.Name;
    

    This should definitely get you in the right direction, but I can’t guarantee this even compiles. The idea is that you can nest queries in LINQ to SQL.

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

Sidebar

Related Questions

UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
There are several rank posts out there but I have yet to see one
I'm trying to make authorization based on owner of post. I store in one
EDIT 2 Okay, based on the advice on the answers below I eliminated my
I have a dialog-based app with an edit-control in it. When I minimize /
My DAL is based on Entity Framework Code First. I have a post model
I have got two models, seen below, and am trying to insert one of
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have the same problem as described in the posts listed below. That is,
Having trouble grasping this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Movie movieToEdit) { var originalMovie = (from

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.