Question

With the method parameter

Int16? id

And the Linq to SQL where clause

where !id.HasValue || m.Id == id

The resulting command text for the condition in the data context is

From the visualizer:

SELECT [t0].[Id], [t0].[Name], [t0].[IsActive]
FROM [Model] AS [t0]
WHERE (CONVERT(Int,[t0].[Id])) = @p0
-------------------------------
@p0 [Int32]: 5

My mapped class has the Id as an Int16 and the database itself has the column type as a smallint, so why does the behind the scenes sql think the parameter is an integer (Int32) and not a smallint (Int16)?


Column mapping:

    [Column(IsPrimaryKey = true, DbType="SmallInt NOT NULL", CanBeNull=false)]
    public Int16 Id { get; set; }
Was it helpful?

Solution

Change the where clause to read

where !id.HasValue || m.Id == id.Value

There is something about the nullable short that is throwing it off. I'm not sure why, but I ran into this before and found adding .Value will work.

OTHER TIPS

Hmmm... I notice that you're not getting any representation of the !id.HasValue in the sql. Perhaps this is some sort of trickery related to that getting wrapped? Seems phony to me, but the only thing i can think of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top