views:

410

answers:

5

This Linq to SQL query ...

    Return (From t In Db.Concessions Where t.Country = "ga" Select t.ConcessionID, t.Title, t.Country)

... is generating this SQL:

SELECT [t0].[ConcessionID], [t0].[Title], [t0].[Country]
FROM [dbo].[Concessions] AS [t0]
WHERE [t0].[Country] = ga

... when what I want is

WHERE [t0].[Country] = 'ga'

Any ideas why?

A: 

the select should be

Select New With {t.ConcessionID, t.Title, t.Country}

rather than

Select t.ConcessionID, t.Title, t.Country

you need to create a new instance of an anonymous type to contain the fields.

Jeff Mc
No, it still is generating the exact same SQL.
Herb Caudill
Only other thing I have is double check Concession.Country is of the correct type in both VB and in the database.
Jeff Mc
It was of the correct type - char(2) - but changing it to nvarchar(50) fixed the problem. Strange.
Herb Caudill
+1  A: 

Wild guess.... could it be?

t.Country = "ga"

vs.

t.Country == "ga"

mspmsp
A: 

Actually, it shouldn't be generating either of those. It would convert the constant into a parameter, so it would be:

SELECT [t0].[ConcessionID], [t0].[Title], [t0].[Country]
FROM [dbo].[Concessions] AS [t0]
WHERE [t0].[Country] = @p0

with @p0 passed as 'ga'. could that be that you are seeing?

James Curran
Nope, I copied and pasted the execution SQL straight from the debugger in VS.
Herb Caudill
I've just duplicated your query as closely as I could, use the Northwind database and LINQPad. It produced the correct SQL. My only suggestion is that there must be something screwy with either the database itself, or the DataContext created from it.
James Curran
A: 

Answer

The Country field was a char(2); I changed it to nvarchar(50), and that fixed the problem. Is this a bug in Linq to SQL?

Herb Caudill
In VB, do you need to use single quotes with Chars, like in C# maybe? As in Country = 'GA' ?
Codewerks
@AugustLights: No, that's not the case in VB
Herb Caudill
A: 

I cannot be sure about this particular example, but there is a known bug that causes similar problems with nvarchar(1) fields. It may be related.

There is a bug report here about it and I also wrote about it on my own site too.

Stephen M. Redd