tags:

views:

172

answers:

2

I'm trying to learn a bit of LINQ but I'm having compile issues right off the bat. Is there any specific reason why this won't work?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text;  


namespace HelloLINQ {

    class HelloLINQ
    {
        public static void Main()
        {
            Example1();
        }


        public static void Example1()
        {
            var numbers = new int[] { 1, 5, 3, 7, 3, 8, 9, 3, 6, 6, 2 };
            var under5 = from n in numbers
                         select n;
            foreach (var n in under5)
            {
                Console.WriteLine(n);
            }
        }
    } 
}  

The error is:

Could not find an implementation of the query pattern for source type 'int[]'. 'Select' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?

+2  A: 

Do you have a reference to System.Core in your project? Everything else is correct.

JaredPar
+2  A: 

Well, we have an error message, carefully written by a Microsoft employee to be helpful, so let's look at it.
Could not find an implementation of the query pattern for source type 'int[]'. 'Select' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?
int[] is a basic C# type. Where are the basic C# types located? System.Core. And the error mentions checking for a reference to System.Core. So, is there a reference to System.Core?
To check this:
Solution Explorer->References. In the list, do you see System.Core?
If not, weird, but it's easily addable. Right click on references, look under .NET for System.Core, add it, and voila.

Rubys
`int[]` is not located in `System.Core`. The LINQ extension methods are.
Matti Virkkunen
care to share where is it located then?
citronas
@Matti: Isn't System.Core the CLR base? Or was that mscorlib?
Rubys
System.Array is in mscorlib.dll This message is looking for Enumerable.Select is in System.Core.dll... or something like Enumerable.Select that works specifically for arrays *spooky music*
David B