Search code examples
c#cakebuild

Cannot find Linq Extension Method in Cake script


I'm trying to use Linq extension methods in my cake script, and I can't get it to find the extension methods.

Here is my script:

#r System.Linq

Task("Default").Does(() => 
{
    var test = new List<string>() {"a", "b", "c"};
    test.OrderByDesc(x => x);
});

RunTarget(target);

I have tried a lot of different ways of referencing System.Link - such as adding .dll or wrapping it on quotes. All of them seem to work. If I make it an incorrect reference, such as System.Link, I will get an error when installing addins when executing the script with "Assembly Not Found".

Here is the full output I get:

PS C:\git\CakeEFTest\CakeTest> ./build.ps1  --experimental
Preparing to run build script...
Running build script...
Analyzing build script...
Processing build script...
Compiling build script...
Error: C:/git/CakeEFTest/CakeTest/build.cake(6,7): error CS1061: 
    'List<string>' does not contain a definition for 'OrderByDesc' 
    and no extension method 'OrderByDesc' accepting a first argument 
    of type 'List<string>' could be found (are you missing a using 
    directive or an assembly reference?)

I get the same results if I run this without the --experimental.

According to the issues in the Cake repository, this should work as expected: https://github.com/cake-build/cake/issues/1331


Solution

  • The LINQ extension method is called .OrderByDescending() https://msdn.microsoft.com/en-us/library/bb548916(v=vs.110).aspx

    Also #r is used for referencing assemblies. For usings, just use using keyword. But System.Linq should be a namespace that's imported by default.