Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 713
Jader Dias Avatar asked Apr 27 '10 02:04

Jader Dias


People also ask

When should we use LINQ?

LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. LINQ stands for Language Integrated Query. LINQ is a data querying API with SQL like query syntaxes. LINQ provides functions to query cached data from all kinds of data sources.

Is LINQ good to use?

Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it. Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources.


2 Answers

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

like image 119
serge_gubenko Avatar answered Sep 18 '22 13:09

serge_gubenko


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.

like image 39
Mark Rushakoff Avatar answered Sep 21 '22 13:09

Mark Rushakoff