Jader Dias
Jader Dias

Reputation: 90475

How to use LINQ in Mono?

I can't make System.Linq (aka LINQ to Objects) work. I am running MonoDevelop 2.2.1 in Ubuntu 10 Lucid Lynx with Mono 2.4.4.

They advertise in their site that they implemented LINQ, but I can't even find Enumerable.Range or ToArray(). What's wrong?

Upvotes: 47

Views: 27765

Answers (3)

serge_gubenko
serge_gubenko

Reputation: 20492

I guess what you would need to do is:

  1. In your project options set "Runtime version" to "Mono/.Net 3.5"
  2. Add reference to System.Core package (right click references in solution explorer)
  3. Add "using System.Linq" to your module

after that your code should compile and execute

hope this helps, regards

Upvotes: 80

Tim Hoolihan
Tim Hoolihan

Reputation: 12396

What do you mean when you say "can't find"? Intellisense? Many of the linq methods are extension methods, and monodevelop may not support those in intellisense. In which case you can still use them and your code should compile, it just isn't in the drop-downs.

About extension methods

Upvotes: 1

Mark Rushakoff
Mark Rushakoff

Reputation: 258208

Are you using the gmcs compiler? mcs does not seem to compile code containing Linq.

$ cat a.cs
using System;
using System.Linq;

class Test
{
    static void Main()
    {
        foreach (var i in new int[] { 1, 2, 3, 4, 5}.Where(n => n % 2 == 0))
        {
            Console.WriteLine(i);
        }
    }
}
$ gmcs a.cs
$ ./a.exe
2
4

To compile with gmcs, perform the following instructions as described by the MonoDevelop FAQ:

Can I compile my project with gmcs?

Yes. Right click on your project, select 'Options'->'Runtime' and select '2.0' from the drop-down list.

Upvotes: 4

Related Questions