Linq to SQL Int16 Gets Converted as Int32 In SQL Command
使用方法参数
以及linq to sql where子句
1
| where !id.HasValue || m.Id == id |
数据上下文中条件的结果命令文本为
从可视化工具:
1 2 3 4 5
| SELECT [t0].[Id], [t0].[Name], [t0].[IsActive]
FROM [Model] AS [t0]
WHERE (CONVERT(Int,[t0].[Id])) = @p0
-------------------------------
@p0 [Int32]: 5 |
我的映射类的id为int16,而数据库本身的列类型为smallint,那么为什么后台SQL认为参数是整数(int32)而不是smallint(int16)?
列映射:
1 2
| [Column(IsPrimaryKey = true, DbType="SmallInt NOT NULL", CanBeNull=false)]
public Int16 Id { get; set; } |
将where子句更改为read
1
| where !id.HasValue || m.Id == id.Value |
空的空头有点让人不快。我不知道为什么,但我之前遇到过这个问题,发现添加.Value是可行的。
- Where(transt(int、[t0][[id])=p0 becomes where[t0][[id]@p0,which is great but@p0 is still an int32 is that normal?
- It also seems best to use this where clause:if(id.haswaloue){qry=qry.where(x======id.value);--(when using.value with the original and when it's actually null it breaks)
- The only remaining question is:why is@p0[int32]and not[int16]
- I've dug through the linq to sql code through reflector.It appears that at some point the id.Value gets translated to a逐字逐句,say 42,that they get treated as an int.C§35;will not consider short as a possibility when they dealing with a逐字逐句。It starts at int and works it's way up.I haven't been able to trace enough of the Code to provide that is what i t is doing but i t seems that way.It also seems to do no harm.
- Thanks for your help Mike Two
六羟甲基三聚氰胺六甲醚。。。我注意到你没有得到任何代表!SQL中的id.hasValue。也许这是某种与包裹有关的诡计?在我看来是假的,但这是我唯一能想到的。
- If id.Hasvalue,then M.Id=id will be evaluated,otherwise the entire condition will be ignored:the where condition will not be present