I have the following code
var x = from e in db.Employees
where e.id == 746
join f in db.CoWorker on e.CoWorkerID equals f.ID into fa
from fr in fa.DefaultIfEmpty()
select new
{
e.id,
e.name,
coWorkerName = fr.FirstName + " " + fr.LastName
};
When i run that code in LINQpad the SQL query looks like
SELECT
[Extent1].[id] AS [id],
[Extent1].[name] AS [name],
CASE WHEN ([Extent2].[ID] IS NULL) THEN CASE WHEN ([Extent2].[FirstName] IS NULL) THEN N'' ELSE [Extent2].[LastName] END + N' ' + CASE WHEN ([Extent2].[LastName] IS NULL) THEN N'' ELSE [Extent2].[LastName] END END AS [C1]
FROM [dbo].[Employees] AS [Extent1]
LEFT OUTER JOIN [dbo].[CoWorker] AS [Extent2] ON [Extent1].[CoWorker] = [Extent2].[ID]
WHERE 1615 = [Extent1].[id]
How do i get rid of the CASE WHEN block?
Here's a query which performs the concatenation client-side instead. As you can see, it's more long-winded - you should absolutely check that it makes a significant difference before you decide to embrace it:
var dbQuery = from e in db.Employees
where e.id == 746
join f in db.CoWorker on e.CoWorkerID equals f.ID into fa
from fr in fa.DefaultIfEmpty()
select new { e.id, e.name, fr.FirstName, fr.LastName };
var query = dbQuery.AsEnumerable()
.Select(x => new {
x.id,
x.name,
coWorkerName = x == null
? ""
: x.FirstName + " " + x.LastName
});
See more on this question at Stackoverflow