开发者

Strange LINQ Error When Using A Variable But Not a Hard-coded String

I have a simple LINQ query running on top of Entity Framework (v1) and pointing at SQL Server Compact v3.5 SP2 (8085):

var myResults = (from m in myContext.MyData
                 join o in myContext.SomeOtherData on new { m.ID, Name = myNameVariable } equals new { o.ID, o.Name }
                 select m).ToArray();

The above query will fail with the following error:

The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ]

If I change the query by simply replacing the myNameVariable with a hard-coded string, it works. For instance:

var myResults = (from m in myContext.MyData
                 join o in myContext.SomeOtherData on new { m.ID, Name = "SomeNameValue"} equals new { 开发者_如何学编程o.ID, o.Name }
                 select m).ToArray();

What is going on here? I am 100% positive that myNameVariable is set to a valid string. It is definitely not null.


The EF generates different SQL for these queries.

For the hard-coded string, it uses a string literal in SQL.

For the variable, it uses a SQL parameter, to defend against SQL injection (no risk with the hard-coded string, unless you do it to yourself!).

So SQL CE must be treating these two differently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜