简体   繁体   中英

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. 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).

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.

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.

I looked at following post and if I change the d.CandidateType to d.CandidateType.Value then code builds correctly.

My question is why the code builds in VS2010 and not in VS2012 no my machine?

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. Above information is retrived using CSC /? command 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.

Nowadays the compiler will use the Type and Name of the referenced members to determine which anonymous type it needs to generate.

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.

The first anonymous type it will generate likely has a CandidateId and CandidateType with types int and 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.

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 . So your proposed solution of going d.CandidateType.Value might be dangerous. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM