none
Dynamic Linq To Sql runtime error RRS feed

  • Question

  • I want to execute a Linq query dynamically. I get a runtime exception that "in" is an invalid term. Here is the code to build the class/method.

    sbClass

    .Append("using System; using System.Collections.Generic; using System.Data; ");
    sbClass
    .Append("using System.Data.Linq; using System.Windows.Forms; ");
    sbClass
    .Append("using System.Data.Linq.Mapping; using System.Text; using System.Data.Sql; ");
    sbClass
    .Append("namespace LinqToSqlPoC { public partial class RuntimeAccess { public RuntimeAccess() {} ");
    sbClass
    .Append("public static BindingSource GetRunTimeData(PcsDataBase pcs, bool mpFormat) {");
    //sbClass.Append(" MessageBox.Show(\"Hello world\"); ");
    sbClass.Append("var query = from sys in pcs.Systems select new { sys.SysCode }; ");
    sbClass
    .Append("BindingSource bs = new BindingSource(); ");
    sbClass
    .Append("bs.DataSource = query; ");
    sbClass
    .Append("return bs; ");
    sbClass
    .Append("} } } ");


    Here are the added references for the compiler on top of the usual stuff you'd need:

    parms
    .ReferencedAssemblies.Add(@"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5
           System.Data.Linq.dll"
    );

    parms

    .ReferencedAssemblies.Add("System.Windows.Forms.dll");


    When I invoke I use:

    AssemblyName
    assemName = assem.GetName();
    Type [] t = assem.GetTypes();
    MethodInfo [] mi = t[0].GetMethods();
    Object ret = t[0].InvokeMember("GetRunTimeData", BindingFlags.InvokeMethod,
           null, null, new Object[] { pcs, mpFormat });


    pcs is a DataContext class that contains my entity classes and mpFormat is a string. I know the compilation works because if I uncomment the "Hello world" line and comment the query it works fine and a Messagebox cmoes up. I have been able to run other static queries on this DataContext prior to trying the dynamic compilation.

    Any ideas why it won't work?

    Monday, June 8, 2009 3:16 PM

Answers

  • Two things.

    1. You are likely not specifying the compiler version option to the CSharpCodeProvider you instantiate, so it thinks you are compiling C# 2.0:

    CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

     

     



    2. You'll also need to add a reference to System.Core.dll.

    Afterwards your dynamic compilation should work.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Thursday, June 11, 2009 2:59 PM
    Answerer

All replies

  • Here is a better formatted post now that I have Firefox opening a code window for me when i ask it too...


    Here is the dynamic code:

    sbClass.Append("using System; using System.Collections.Generic; using System.Data; "
    );
    sbClass.Append("using System.Data.Linq; using System.Windows.Forms; " );
    sbClass.Append("using System.Data.Linq.Mapping; using System.Text; using System.Data.Sql; " );
    sbClass.Append("namespace LinqToSqlPoC { public partial class RuntimeAccess { public RuntimeAccess() {} " );
    sbClass.Append("public static BindingSource GetRunTimeData(PcsDataBase pcs, bool milePostFormat) { " );
    //sbClass.Append(" MessageBox.Show(\"Hello world\"); ");
    sbClass.Append("var query = from sys in pcs.Systems select new { sys.SysCode }; " );
    sbClass.Append("BindingSource bs = new BindingSource(); " );
    sbClass.Append("bs.DataSource = query; " );
    sbClass.Append("return bs; " );
    sbClass.Append("} } } " );

    Here are some compiler parameters:

    parms.ReferencedAssemblies.Add("System.dll"
    );
    parms.ReferencedAssemblies.Add("System.Data.dll" );
    parms.ReferencedAssemblies.Add(@"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll" );
    parms.ReferencedAssemblies.Add("System.Windows.Forms.dll" );


    and here is how I call it:

    CompilerResults results = provider.CompileAssemblyFromSource(parms, SourceCode);

    if (results.Errors.Count > 0)
    foreach (CompilerError ce in results.Errors)
    {
    MessageBox.Show(ce.ToString());
    }
    else
    {
    Assembly assem = Assembly.LoadFile(Application.StartupPath + "\\dyn.dll" );
    AssemblyName assemName = assem.GetName();

    Type [] t = assem.GetTypes();
    MethodInfo [] mi = t[0].GetMethods();
    Object ret =t[0].InvokeMember("GetRunTimeData" ,BindingFlags.InvokeMethod,
    null , null , new Object[] { pcs, milePostFormat });

    }
    Any ideas why it cannot resolve the in?

    .Net 3.5 framework and Sql express 2008
    Monday, June 8, 2009 7:01 PM
  • Any takers?
    Tuesday, June 9, 2009 1:44 PM
  • Two things.

    1. You are likely not specifying the compiler version option to the CSharpCodeProvider you instantiate, so it thinks you are compiling C# 2.0:

    CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

     

     



    2. You'll also need to add a reference to System.Core.dll.

    Afterwards your dynamic compilation should work.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    Thursday, June 11, 2009 2:59 PM
    Answerer
  • Thanks. You are correct Bin-ze came up with the answer on another thread. I was using the CodeDomProvider class which did not have to option to set the correct version. Once I used CSharpCodeProvider it worked.
    Thursday, June 11, 2009 3:06 PM