tags:

views:

79

answers:

2
+2  A: 

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!

spender
Might have been better to mention this in original post. Have provided alternative statement that should do the trick.
spender
The reason your statement fails is because you are performing the "Where" clause on a set that no longer contains "Year". It only contains "Record" and "DisplayName". Reordering the statement does the trick.
spender
I think this would be better continued in a new question :)
spender
I updated the question.. I think it's part of it too :) thank you for your help so far!!!!
Alex
I need to keep the return type the same (IQueryable<domain object>) because the query is then later potentially enhanced with other IQueryable extension methods. The only thing I could do is add a new property to the domain object and dynamically assign to it (instead of the new anonymous type + property). Is that possible?
Alex
Is possible to change the table? You might want to consider a computed column that calculates the DisplayName, which would really nip the problem at the root.
spender
http://stackoverflow.com/questions/tagged/computed-columns
spender
Yes I could edit the table. I'll try that approach! Otherwise, is it possible to maybe use the anonymous type solution you provided, and then return only the subobject and thereby preserving the return type? I don't need the actual displayname from the DB, that's only for ordering purposes.
Alex
This looks like it might help: http://blog.dmbcllc.com/2008/02/12/computed-columns-using-linq-to-sql-classes/
spender
I get it now. Check the last part of the answer.
spender
A: 

This isn't answering your question using LINQ to SQL but it might be a better idea to create a VIEW and use the COALESCE operator to pick the firt non null value from either Name or CompanyName.

Kane