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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:37:30+00:00 2026-05-10T23:37:30+00:00

I have 2 tables which in simplified form look like this: Products( id: int,

  • 0

I have 2 tables which in simplified form look like this:

Products(    id: int,    name: varchar );  ProductSpecs(    product_id: int,    spec_name: varchar,    spec_value: int ); 

Now I need to sort products (in linq to sql) by value of some specification item (eg. ‘price’). So I do something like this

var products = from p in db.Products                from ps in p.ProductsSpecs                where ps.spec_name == 'price'                orderby ps.spec_value                select p; 

The problem is that if there’s no such ProductSpec with spec_name ‘price’ the product is not included at all. I can add these products with Union or Concat but this way the sorting of the first part is not preserved.

What is the best way to deal with this?

Thanks.

  • 1 1 Answer
  • 2 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. 2026-05-10T23:37:30+00:00Added an answer on May 10, 2026 at 11:37 pm

    First, I would recommend that you either do this in pure SQL as a function or Stored Procedure and then access this through linq, or add a price column to your product table. It seems like price would be a normal attribute to add to all of your products even if that price is NULL.

    SQL:

    select p.* from products p left outer join productspecs ps on     p.id = ps.product_id     and ps.spec_name = 'Price' order by ps.spec_value 

    With that said, here’s the weird bit of LINQ that should work on your table (I might have some of the column names spelled incorrectly):

    var products = from p in db.Products                join ps in (from pss in db.ProductSpecs                            where pss.spec_name== 'Price'                            select pss                            ) on p.id equals ps.product_id into temp                from t in temp.DefaultIfEmpty()                orderby t.spec_value                select p; 

    I tested this on some tables setup like above and created 5 products, three with prices in different value orders and this LINQ ordered them just like the SQL above and returned the null result rows as well.

    Hope this works!

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

Sidebar

Related Questions

I have 2 related database tables which in simplified form look like this Product(
I have two tables which looks something like this Table Queue int ID; string
I have a table which contains TV Guide data. In a simplified form, the
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have a product table which simplifies to this: create table product(id int primary
I have two tables A,B which are related to each other (simplified): A: +-------+---------+
I have the following db tables (which is simplified to illustrate the problem) CampaignTx
So I have two tables in this simplified example: People and Houses. People can
I have the following MySQL tables (Simplified) DRIVER (D) ---------- id (PK) name RACE
I have 3 tables which need to be linked in an SQL statement (I'm

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.