[英]System.Linq Linq queries not working in VS2012 but works fine in VS2010
I have a linq query that works fine in VS2010 and VS2012 on system where code was originally, written but when commited to SVN, and pulled to new system, Code does not built. 我有一个linq查询,该查询在VS2010和VS2012上可以正常运行,该系统中的代码最初是编写的,但是在提交到SVN并拉到新系统时,没有构建代码。 The same code works fine in 2 systems but fails in other 3 system each of them having VS2012, .NET Framework 4.0 (Target Framework). 相同的代码在2个系统中均能正常工作,但在其他3个系统中均具有VS2012,.NET Framework 4.0(目标框架)的系统中均无法运行。
Not sure what's wrong with the code. 不确定代码有什么问题。 I understand the code written is having bugs and is written terribly wrong way. 我了解所编写的代码存在错误,并且编写方式非常错误。
Here is sample linq query that works fine in VS2010 but fails in VS2012. 这是样例linq查询,该查询在VS2010中工作正常,但在VS2012中失败。
from d in db.VacancyCandidates
join c in db.Candidates on new
{ CandidateId = d.CandidateId, CandidateType = d.CandidateType}
equals new { CandidateId = c.CandidateId, CandidateType = c.CanidateTypeId }
........Rest query ......
From above query d.CandidateType
is Nullable<int>
and c.CanidateTypeId
is int. 从上面的查询中, d.CandidateType
为Nullable<int>
而c.CanidateTypeId
为int。
I looked at following post and if I change the d.CandidateType
to d.CandidateType.Value
then code builds correctly. 我看了看下面的帖子 ,如果我改变d.CandidateType
到d.CandidateType.Value
然后代码生成正确。
My question is why the code builds in VS2010 and not in VS2012 no my machine? 我的问题是为什么代码不是在我的机器上构建在VS2010中,而不是在VS2012中构建?
Adding further screenshots which may help you. 添加更多屏幕截图可能会对您有所帮助。
More information related compilers: 有关编译器的更多信息:
Pls. 请 know that on all system, the target framework for the project is 4.0 only. 知道在所有系统上,该项目的目标框架仅是4.0。 Above information is retrived using CSC /?
以上信息使用CSC /?
检索CSC /?
command C:\\Windows\\Microsoft.NET\\Framework64\\V4.0.3.319\\
命令C:\\Windows\\Microsoft.NET\\Framework64\\V4.0.3.319\\
I can't speak to the why it works in VS2010 in a, most likely, older version of the C# compiler. 我无法说说为什么它可以在VS2010中使用(很可能是较旧的C#编译器)版本的原因。
Nowadays the compiler will use the Type
and Name
of the referenced members to determine which anonymous type it needs to generate. 如今,编译器将使用引用成员的Type
和Name
来确定它需要生成哪种匿名类型。
It will try to match up any anonymous types which have exactly the same signature. 它将尝试匹配具有完全相同签名的所有匿名类型。 So new { A = "1" }
and new { A = "2" }
will have the same type, but new { A = "1" }
and new { A = 1 }
will not. 因此, new { A = "1" }
和new { A = "2" }
将具有相同的类型,但是new { A = "1" }
和new { A = 1 }
将没有相同的类型。
The first anonymous type it will generate likely has a CandidateId
and CandidateType
with types int
and int?
它会生成的第一个匿名类型可能是CandidateId
和CandidateType
类型为int
和int?
respectively. 分别。 For the second anonymous type, it will verify if any other types have that exact same signature. 对于第二种匿名类型,它将验证是否还有其他类型具有完全相同的签名。 Which isn't the case here, as CandidateType
is of type int
in the second initializer. 此处不是这种情况,因为CandidateType
在第二个初始化程序中为int
类型。
As said, in order to have the signatures match, you need to ensure the types align. 如前所述,为了使签名匹配,您需要确保类型对齐。
from d in db.VacancyCandidates
join c in db.Candidates on new { CandidateId = d.CandidateId, CandidateType = d.CandidateType}
equals new { CandidateId = c.CandidateId, CandidateType = (int?)c.CanidateTypeId }
Beware that consuming the value of a Nullable<>
type will fail when the value is null
. 请注意,如果值为null
,则使用Nullable<>
类型的值将失败。 So your proposed solution of going d.CandidateType.Value
might be dangerous. 因此,您建议使用d.CandidateType.Value
解决方案可能很危险。 It " Might " be dangerous because dependent on whether you are directly querying a database via Linq-to-SQL or EntityFramework, or working in memory the behavior is different. “ 可能 ”很危险,因为取决于您是通过Linq-to-SQL还是EntityFramework直接查询数据库,还是在内存中工作,行为是不同的。
Consuming the .Value
of a null
Nullable<>
will thrown a InvalidOperationException
when executed as an in memory lambda, but will not in case it is translated to a SQL query, as the QueryProvider
is smart enough to fix this. 当作为内存中的lambda执行时,使用null
Nullable<>
的.Value
将引发InvalidOperationException
,但不会将其转换为SQL查询,因为QueryProvider
很聪明,可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.