Search code examples
c#linqroslyn

CompilationErrorException when including System.Linq in Roslyn CP2


I have downloaded the Roslyn CTP and have run across the following error.A CompilationErrorException is thrown when executing the line session.Execute(@"using System.Linq;"); with the following message:

(1,14): error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)

My code is:

namespace RoslynError
{
    using System;
    using Roslyn.Scripting;
    using Roslyn.Scripting.CSharp;

    internal class RoslynError
    {
        static void Main(string[] args)
        {
            var engine = new ScriptEngine();
            Session session = engine.CreateSession();
            session.Execute(@"using System.Collections;");
            session.Execute(@"using System.Linq;");
            Console.ReadKey();
        }
    }
}

I'm especially confused as to why the System.Linq line throws an error while System.Collections is fine.


Solution

  • The engine needs a reference to the assembly that the System.Linq namespace is in (System.Core.dll)

    engine.AddReference(typeof(System.Linq.Enumerable).Assembly.Location);
    

    This needs to be done before the session is created.