tags:

views:

378

answers:

3

I need some help with a LINQ query in VB.Net, please.

I have this simple group statement:

Dim drivers = From d In DriversOwners _
   Group d By Key = d.UnitNumber Into Group _
   Select Key, DriverGroup = Group

This works, and returns me the data I need to work with. Now I want to iterate through the groups, using a For Each construct. like this:

For Each x In drivers

Next

However, the compiler is barking at me, telling me that the

"'x' is not accessible in this context because it is 'Friend'."

Anyone know what I am doing wrong here?

Thanks in advance.

+2  A: 

Looks to me like the variable x was declared earlier - as a class field, a method parameter, or a local variable.

Am I right?

Christian Madsen
Nope - I used "x" on purpose, because it is not used anywhere else. In fact, it doesn't matter what single-letter variable I use in place of "x," I get the same error.When I try to replace "x" with a word, like "foo" I get a different error: "Name 'foo' is not declared."
camainc
Just for curiosity can you change the for each line to "For Each x As Object In drivers" and see if that gives you an error? I've reproduced a simple version of your code and used the exact LINQ statement you provided and can't get it to raise that error.
Chris Haas
Well that works, of course, but I can't do anything with an object, besides the five or so default object methods.It seems like I might be missing an assembly reference or something. This is a .Net 2.0 project that I changed to target .Net 3.5. I added all the LINQ references, but still something seems to be not right.
camainc
+3  A: 

After digging and digging, I finally found the answer to this problem. Talk about obtuse!

Enabling LINQ in a .NET Framework 3.5 Project

When you move a project to .NET Framework 3.5, a reference to System.Core and a project-level import for System.Linq (in Visual Basic only) are added automatically. If you want to use LINQ features, you must also turn Option Infer on (in Visual Basic only) [my emphasis].

When I changed the target framework from 2.0 to 3.5, Visual Studio automatically added the System.Core assembly, and automatically imported the System.Linq namespace. Now why in the world did it not also set Option Infer to "On" as well?

http://msdn.microsoft.com/en-us/library/bb398197.aspx

camainc
Congrats on solving your problem. In answer to your question see the note here http://msdn.microsoft.com/en-us/library/bb384665.aspx which states "If you do not specify On or Off, the default is On for projects created in Visual Basic 2008. The default is Off for projects upgraded from earlier versions."
Ahmad Mageed
Well, I thought that solved the problem, but no. The original problem is still there.
camainc
Thanks, Ahmad, appreciate that.
camainc
+1  A: 

I'm not sure if you've solved this or not. I had the exact same problem today and what ended up working for me was to not use a single-letter variable in the For Each loop. My code was the same as yours:

For Each x In a
 ...
Next

When I changed the code to the following it worked:

For Each retVal in a
 ...
Next

I also found the same 'Friend' error behavior for any single-letter variable.

I have no idea why it behaves this way, but I thought I'd pass this along in case this question is still out there.

bcarroll2k3
Thanks, that's what I ended up doing as well. I'll mark your answer as the correct one, since it's the only way I was able to get that code to work.
camainc