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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:13:45+00:00 2026-05-14T20:13:45+00:00

I have a news table and I would like to implement custom ordering. I

  • 0

I have a news table and I would like to implement custom ordering. I have done this before via a positional mapping table which has newsIds and a position.

I then LEFT OUTER JOIN the position table ON news.newsId = position.itemId with a select case statement

CASE WHEN [position] IS NULL THEN 9999 ELSE [position] END

and order by position asc, articleDate desc.

Now I am trying to do the same with Linq to Entities. I have set up my tables with a PK, FK relationship so that my News object has an Entity Collection of positions.

Now comes the bit I can’t work out. How to implement the LEFT OUTER JOIN.

I have so far:

 var query = SelectMany (n => n.Positions, (n, s) => new { n, s })
                    .OrderBy(x => x.s.position)
                    .ThenByDescending(x => x.n.articleDate)
                    .Select(x => x.n);

This kinda works. However this uses a INNER JOIN so not what I am after.

I had another idea:

 ret = ret.OrderBy(n => n.Positions.Select(s => s.position));

However I get the error DbSortClause expressions must have a type that is order comparable.

I also tried

ret = ret.GroupJoin(tse.Positions, n => n.id, s => s.itemId, (n, s) => new { n, s })
                    .OrderBy(x => x.s.Select(z => z.position))
                    .ThenByDescending(x => x.n.articleDate)
                    .Select(x => x.n);

but I get the same error!

If anyone can help me out, it would be much appreciated!

UPDATE:
So after some playing about I have managed to get it working.

ret = ret.GroupJoin(entity.Positions, n => n.id, s => s.itemId, (n, s) => new { n, s })
                    .SelectMany(x => x.n.Positions.DefaultIfEmpty(), (n, s) => new { n, s })
                    .OrderBy(x => x.s.position)
                    .ThenByDescending(x => x.n.n.articleDate)
                    .Select(x => x.n.n);

However, this is still not completely right. I have no way of using only a specific positionid or articleType.

If I have news id 1 and a review id but defined in the positions table, currently (I think) the linq query would select both?

If I try and use a where clause, it’s basically the same as inner joining. What I need is to try and use a case in the select like I would do in straight SQL:

CASE WHEN [position] IS NULL OR shuffleId != 1 THEN 9999 ELSE [position] END

I may have to completely rethink how do this. Don’t suppose anyone has any suggestions of alternative methods?

  • 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-14T20:13:46+00:00Added an answer on May 14, 2026 at 8:13 pm

    How about:

    var query =
       from n in news
       let p = n.Positions.Select(p=>p.Position).FirstOrDefault() ?? 9999
       orderby p, n.ArticleDate
       select n;
    

    Ps. the above assumes there is only 1 position entry for each news … just seems weird if that were not the case.


    Solution posted by OP in the comment below:

    ret = ret
          .GroupJoin(tse.Positions, n => n.id, s => s.itemId, (n, s) => new { n, s }) 
          .SelectMany(x => x.s.DefaultIfEmpty(), (n, s) => new { n, s }) 
          .OrderBy(x => x.s.position == null || x.s.positionId != positionId ? 9999 : x.s.position) 
          .ThenByDescending(x => x.n.n.articleDate) 
          .Select(x => x.n.n);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing news posts and would like to get the entire
I have three tables in my database. News, reviews and tutorials. I would like
Given you have to implement a news feed like the one seen in social
I have a SQL table with news stories and Unix timestamps. I'd like to
In my news web page I would like to have a related news using
I have a database table containing the news on my site. It's divided into
I would like to perform LINQ to SQL mapping in C#, in a many-to-many
I have the following xml that I would like to read: chinese xml -
i have in Symfony 1.4 and Doctrine 1.2 such query: $this->query = Doctrine_Core::getTable('News') ->createQuery('a')->leftJoin('Tag
I have two tables in my database: NEWS table with columns: id - the

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.