SQL Coalesce in Linq syntax

D

Dean Slindee

How would the following SQL Coalesce statement need to be written in Linq to
SQL:
SELECT COALESCE(LastName, '') + ' '
+ COALESCE(FirstName, '') + ' '
+ COALESCE(MiddleName, '') + ' '
+ COALESCE(SuffixName, '') AS ClientName,
ClientID
FROM tblClient
WHERE LastName LIKE @Letter + '%'
ORDER BY ClientName

Thanks,
Dean Slindee
 
F

Frans Bouma [C# MVP]

Dean said:
How would the following SQL Coalesce statement need to be written in
Linq to SQL: SELECT COALESCE(LastName, '') + ' '
+ COALESCE(FirstName, '') + ' '
+ COALESCE(MiddleName, '') + ' '
+ COALESCE(SuffixName, '') AS ClientName,
ClientID
FROM tblClient
WHERE LastName LIKE @Letter + '%'
ORDER BY ClientName

Thanks,
Dean Slindee

Use the '??' operator in C#

so:
var q= from c in nw.Customer
select new
{
FirstName = c.FirstName??''
//...

};

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
C

Cor Ligthert [MVP]

Frans,

Zit je je daar een beet je zondag avond uit te sloven of ben je nog niet
vertrokken?

Sorry fellows, had to be in Dutch

Cor
 
D

Dean Slindee

Here is another way in VB:
Dim query = From tblClient In dc.tblClients _
Where tblClient.LastName.StartsWith(s1) _
Order By tblClient.LastName, tblClient.FirstName,
tblClient.MiddleName _
Select ClientName = tblClient.LastName & " " & _
If(tblClient.FirstName, "") & "
" & _
If(tblClient.MiddleName, "") & "
" & _
If(tblClient.SuffixName, ""), _
tblClient.ClientID
 
F

Frans Bouma [C# MVP]

Cor said:
Frans,

Zit je je daar een beet je zondag avond uit te sloven of ben je nog
niet vertrokken?

Ik ga dit jaar niet naar de summit :)

FB
Sorry fellows, had to be in Dutch

Cor


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top