Ambiguous invocation caused by picking up two versions of System.Linq
Asked Answered
H

2

9

I have the following code, which shows a squiggly red line under the lambda expression after .Any( because of an "ambiguous invocation" between System.Linq versions 3.5 and 4.0 - how do I force it to use a particular version?

It compiles and runs fine.

string[] allowedExtensions = { "PNG", "JPG", "JPEG", "GIF" };
string fileExtension = (Path.GetExtension(postedFile.FileName) ?? "NULL").ToUpper().TrimStart(new[] { '.' });

if (this.MediaService.FileAllowed(postedFile) 
    && allowedExtensions.Any(e => e == fileExtension))
{ ... }

UPDATE:

I've now checked all (60) projects in the entire solution and all the references to System.dll and System.Core.dll are version 4.0 - I really can't understand where it's getting the reference to 3.5 from.

Horsehide answered 15/2, 2012 at 17:1 Comment(2)
If it compiles and runs fine, is just a cosmetic problem then? Have you tried fully rebuilding the project?Bathurst
It is just a cosmetic problem, but still annoying. Tried rebuilding with no joy...Horsehide
S
6

You probably have two modules that reference different versions. Search the project files for those references and bring them to consistency (4.0).

Scudder answered 16/2, 2012 at 9:33 Comment(3)
Hi @Bertrand - I've checked all the projects in the entire solution and all the references to System.dll and System.Core.dll are version 4.0 - I really can't understand where it's getting 3.5 from! :(Horsehide
Weird. What do your using statements look like?Scudder
Hi @BertrandLeRoy - looks like a restart of the machine did the trick! Very strange. I'll give you the answer as that's the first thing I did when confronted with the problem. Thanks for your help.Horsehide
P
10

I had this problem after ReSharper offered to 'reference System.Core' and 'import System.Linq' in a file where I was using a Linq expression but hadn't yet imported 'System.Linq'. When I executed the ReSharper command, I got some error that part of it had failed. Then all Linq expressions in my project were ambiguous between System.Core 3.5 and 4.0. Looking at all my System and System.Core references, they were 4.0. Removing the reference to System.Core (4.0) caused the ambiguity errors to go away, and made System.Core disappear from the list of references for that project. When I went back to re-add System.Core, although it did not appear in my list of references in Solution Explorer, it was checked in the 'Add Reference to ' dialog. Attempting to un-check it there and then re-check it resulted in an error that System.Core is already referenced by my project. I'm still fixing errors so I don't know if this will affect the build process.

Pen answered 2/8, 2012 at 12:59 Comment(2)
I ran into the exact same problem with resharper just now and ended up with the same result. I googled the error immediately though, found this question, read the first answer, restarted VS after reading the comments on Bertrand's answer and that fixed it. The build worked just fine immediately after the restart.Novick
@NickLarsen good to know. Even after restart I still have my error where System.Core does not appear in Solution Explorer's list of references, but appears checked in 'Add Reference to ...' dialog, and attempting to uncheck and recheck System.Core there results in error that System.Core is already added. I wish I had just gone with the restart initially! When I get to a clean build I'll find out if this is a lasting problem for me...Pen
S
6

You probably have two modules that reference different versions. Search the project files for those references and bring them to consistency (4.0).

Scudder answered 16/2, 2012 at 9:33 Comment(3)
Hi @Bertrand - I've checked all the projects in the entire solution and all the references to System.dll and System.Core.dll are version 4.0 - I really can't understand where it's getting 3.5 from! :(Horsehide
Weird. What do your using statements look like?Scudder
Hi @BertrandLeRoy - looks like a restart of the machine did the trick! Very strange. I'll give you the answer as that's the first thing I did when confronted with the problem. Thanks for your help.Horsehide

© 2022 - 2024 — McMap. All rights reserved.