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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:22:20+00:00 2026-05-12T13:22:20+00:00

I have a database with a table that contains Name and CompanyName . They

  • 0

I have a database with a table that contains Name and CompanyName. They are mutually exclusive (either field is null). Using LINQ to SQL, I’m trying to order these two “into” each other (but not in-memory, I would like this done by SQL Server 2008) so that I can get a single column “DisplayName” which will do this:

Record     Name       CompanyName
     1     Smith      NULL
     2     Fields     NULL
     3     NULL       Microsoft
     4     Zian       NULL
     5     NULL       Gibbons

My output should be

Record     DisplayName
     2     Fields
     5     Gibbons
     3     Microsoft
     1     Smith
     4     Zian

Note how the output is ordered across both source columns. This needs to support pagination (skip & take). Any idea how to do this?

Thanks to spender I have this now:

db
.Table
.Where(t=>t.Year>1990)
.Select(t=>new{t.Record,DisplayName=t.Name??t.CompanyName})
.OrderBy(t.DisplayName);

The remaining problem is: How can I still return only the original domain objects? The anonymous type introduced in above query now throws errors that the return type is wrong (which has to stay (IQueryable)). Almost there?!.. Basically I need to find a way to assign the “DisplayName” value sorted for to the original object during the query.

  • 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-12T13:22:20+00:00Added an answer on May 12, 2026 at 1:22 pm

    EDIT:

    db
    .Table
    .Select(t=>new{t.Record,DisplayName=t.Name??t.CompanyName})
    .OrderBy(t.DisplayName); 
    

    Would create SQL similar to:

    SELECT [t1].[Record], [t1].[DisplayName]
    FROM (
        SELECT 
            [t0].[Record], 
            COALESCE([t0].[Name],[t0].[CompanyName]) AS [DisplayName]
        FROM [Table] AS [t0]
        ) AS [t1]
    ORDER BY [t1].[DisplayName]
    

    This looks like it fulfills your requirements. You could happily tack Skip and Take to the end of this statement to support pagination.

    To account for Year (as per comment):

    db
    .Table
    .Where(t=>t.Year>1990)
    .Select(t=>new{t.Record,DisplayName=t.Name??t.CompanyName})
    .OrderBy(t.DisplayName); 
    

    You last requirement would be as simple as writing a new class to encapsulate the result:

    public class Result
    {
        public int Record
        {
            get;
            set;
        }
    
        public string DisplayName
        {
            get;
            set;
        }
    }
    

    Then the query would become:

    db
    .Table
    .Where(t=>t.Year>1990)
    .Select(t=>new Result{t.Record,DisplayName=t.Name??t.CompanyName})
    .OrderBy(t.DisplayName); 
    

    However, I am starting to think that if you were to create a view of the SQL query:

    SELECT [t1].[Record], [t1].[DisplayName],[t1].[Year]
    FROM (
        SELECT 
            [t0].[Record], 
            COALESCE([t0].[Name],[t0].[CompanyName]) AS [DisplayName],
            [t0.Year]
        FROM [Table] AS [t0]
        ) AS [t1]
    ORDER BY [t1].[DisplayName]
    

    and then to drag this view onto your Dbml surface, you might get better mileage.

    EDIT:

    If you can, the optimal approach would be to alter the source table as follows, so that DisplayName becomes a computed column at source.

    ALTER TABLE [Table]
        ADD [DisplayName] As 
        (COALESCE([Name],[CompanyName]))
    

    I finally grok your question (I didn’t realise DisplayName was only for ordering purposes, and not required to be maintained). Here’s your query:

    db
    .Table
    .Where(t=>t.Year>1990)
    .Select(t=>new {Item=t,DisplayName=t.Name??t.CompanyName})
    .OrderBy(a=>a.DisplayName)
    .Select(a=>a.Item);
    

    TADA!

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

Sidebar

Related Questions

I have a table like these: Club Table contains: ClubID(IDENTITY), Name, Address Genre table
I have a database in Access 2003 that I only want certain people to
I have a class that contains hierarchical data. I want to present this data
I have a directory: Audio/ and in that will be mp3 files only. I'm
I volunteer for a local non-profit that gives out toys and food during the
For my CMS application I'm writing a DLL that I will include into my
I'm currently doing a summer job and I have to extend an existing program.
I am building a customer sales and invoicing app for a company.The app is
I am building a web app, and I am thinking about how I should
I need to mirror all of the DNS entries on our DNS server in

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.