简体   繁体   English

System.Linq Linq查询无法在VS2012中工作,但在VS2010中可以正常工作

[英]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.CandidateTypeNullable<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.CandidateTyped.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. 如今,编译器将使用引用成员的TypeName来确定它需要生成哪种匿名类型。

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? 它会生成的第一个匿名类型可能是CandidateIdCandidateType类型为intint? 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.

相关问题 从vs2010迁移到vs2012后,Linq包含不起作用 - Linq Contains not working after migrate from vs2010 to vs2012 AppFabric DataCacheFactory()初始化在VS2013中挂起,在VS2010和VS2012中正常工作 - AppFabric DataCacheFactory() initialization hangs in VS2013, works fine in VS2010 and VS2012 SQL Top 1与System.Linq firstordefault - sql Top 1 vs System.Linq firstordefault 为什么数据驱动的单元测试在vs2012中在vs2010中正常工作时失败? - Why does data driven unit test fail in vs2012 when it worked fine in vs2010? System.Linq.Dynamic.ParseException:&#39;。&#39; 或&#39;(&#39;预期-VS2012错误 - System.Linq.Dynamic.ParseException: '.' or '(' expected - VS2012 ERROR VS2012 OutOfMemoryException与VS2010没有异常的工作 - VS2012 OutOfMemoryException vs. VS2010 working w/o Exception Moq和Interop类型:在VS2012中工作,在VS2010中失败? - Moq & Interop Types: works in VS2012, fails in VS2010? DeploymentItem在VS2010和VS2012中的行为方式不同 - DeploymentItem behaving differently in VS2010 and VS2012 如何在VS2010和VS2012中加载数据库项目? - How to load database project in VS2010 and VS2012? 将SQL语句转换为LINQ VS2010 - Convert SQL Statement to LINQ VS2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM