Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string)

后端 未结 2 869
情深已故
情深已故 2021-01-18 16:22

I\'m using the Mono.CSharp library to emit code. Following another question on SO (http://stackoverflow.com/questions/3407318/mono-compiler-as-a-service-mcs) I managed to g

2条回答
  •  鱼传尺愫
    2021-01-18 17:01

    I didn't try this, but I guess you could use Mono compiler to create a delegate vthat takes IQueryable as argument and returns the filtered query. Something like:

    IQueryable contacts = GetContacts(); 
    string query = "new Func, IQueryable>(contracts =>
                      from contact in contacts 
                      where contact.Name == \"name\" 
                      select contact)"; 
    var res = Mono.CSharp.Evaluator.Evaluate(query); 
    

    Then you just need to cast res to an appropriate Func<,> type and invoke it to get the result.

提交回复
热议问题