Take the following Linq query
from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID into og
from o in og.DefaultIfEmpty()
select new
{
c.CompanyName,
HasOrdered = (o.CustomerID != null) ? "Yes they have" : "No they havent"
}
When run, I get a 'Operator 'NotEqual' isn't supported.' error. Ok, so obviously I can make it == and flip the responses, but there are times where it might be needed. So, I tried the following:
from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID into og
from o in og.DefaultIfEmpty()
select new
{
c.CompanyName,
HasOrdered = (!(o.CustomerID == null)) ? "Yes they have" : "No they havent"
}
and I get a Null reference exception, presumably because the order entity is null as it is the result of a left join, although it doesnt seem to get as far as executing any SQL.
so I try:
from c in db.Customers
join o in db.Orders on c.CustomerID equals o.CustomerID into og
from o in og.DefaultIfEmpty()
select new
{
c.CompanyName,
HasOrdered = (!(o == null) && !(o.CustomerID == null)) ? "Yes they have" : "No they havent"
}
which is slightly messy and may be completely the wrong way of going about it, but anyway this generates SQL, but it is broken.
exec sp_executesql N'
SELECT [LPA_L1].[CompanyName],
CASE WHEN [].[LPFA_8] THEN 1 ELSE 0 END AS [LPFA_9],
@LO8c4924a51 AS [LPFA_10],
@LOf34a29412 AS [LPFA_11]
FROM ( [Northwind].[dbo].[Customers] [LPA_L1]
LEFT JOIN [Northwind].[dbo].[Orders] [LPA_L2]
ON [LPA_L1].[CustomerID] = [LPA_L2].[CustomerID])',
N'@LO8c4924a51 nvarchar(13),@LOf34a29412 nvarchar(14)',
@LO8c4924a51=N'Yes they have',
@LOf34a29412=N'No they havent'
with the [] causing it to die.
On another null related note, and maybe this should be in a different thread, but when returning non nullable types (guids, datetimes etc.) in 'select new {}' blocks where they could be null (ie as the result of a left join), Linq to LLBLGen returns the 'empty' defaults for those types (eg. Guid.Empty) whereas Linq to SQL converts them to nullable types, which is what I would (perhaps wrongly) expect.
Thanks again, and I hope at least some of it makes sense...