Figuread it out through logging, these are my results:
I get the following results:
Works:
(from e in CampaignCodes where e.Code.Equals(code) && e.Domain.Equals(null) select e).FirstOrDefault().Dump();
SELECT TOP (1) [t0].[Id], [t0].[Code], [t0].[Domain], [t0].[ClientId], [t0].[CampaignId], [t0].[PathId], [t0].[DescriptionText], [t0].[DeleteFlag], [t0].[Created]
FROM [CampaignCodes] AS [t0]
WHERE ([t0].[Code] = @p0) AND ([t0].[Domain] IS NULL)
-- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8
Doesn't Work:
(from e in CampaignCodes where e.Code.Equals(code) && e.Domain.Equals(domain) select e).FirstOrDefault().Dump();
SELECT TOP (1) [t0].[Id], [t0].[Code], [t0].[Domain], [t0].[ClientId], [t0].[CampaignId], [t0].[PathId], [t0].[DescriptionText], [t0].[DeleteFlag], [t0].[Created]
FROM [CampaignCodes] AS [t0]
WHERE ([t0].[Code] = @p0) AND ([t0].[Domain] = @p1)
-- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p1: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.
8
so
By rewriting my linq2sql to:
(from e in CampaignCodes where e.Code.Equals(code) && ((domain==null && e.Domain.Equals(null))||e.Domain.Equals(domain)) select e).FirstOrDefault().Dump();
I get:
SELECT TOP (1) [t0].[Id], [t0].[Code], [t0].[Domain], [t0].[ClientId], [t0].[CampaignId], [t0].[PathId], [t0].[DescriptionText], [t0].[DeleteFlag], [t0].[Created]
FROM [CampaignCodes] AS [t0]
WHERE ([t0].[Code] = @p0) AND ([t0].[Domain] = @p1)
-- @p0: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [test]
-- @p1: Input NVarChar (Size = 14; Prec = 0; Scale = 0) [mydomain.se]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8
When domain isn't null and:
SELECT TOP (1) [t0].[Id], [t0].[Code], [t0].[Domain], [t0].[ClientId], [t0].[CampaignId], [t0].[PathId], [t0].[DescriptionText], [t0].[DeleteFlag], [t0].[Created]
FROM [CampaignCodes] AS [t0]
WHERE ([t0].[Code] = @p0) AND (([t0].[Domain] IS NULL) OR ([t0].[Domain] = @p1))
-- @p0: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [test]
-- @p1: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8
When it is.
But I'd hoped that there would be a nices methods for this...