I am trying to call LinqPad query from C#. Unfortunately, the code below does not work; the result is null as if nothing got returned by the script. I don't see any example of how to do this online. Any direction would be appreciated.
This is the LinqPad code for the query. It is saved as a C# Statement:
string Main(string message)
{
"testing".Dump();
return message.ToUpper();
}
This is the code in C# code in a Visual Studio Project Console Application that attempts to call the query:
using System;
using LINQPad;
namespace ConsoleAppLinqPad
{
internal class Program
{
static void Main(string[] args)
{
string pathToQuery = @"C:\Users\synct\OneDrive\Documents\LINQPad Queries\";
string script = "samplequery.linq";
var wholePath = pathToQuery + script;
using (var query = Util.Compile(wholePath))
{
var results = query.Run(QueryResultFormat.Text, "hello world").ReturnValueAsync.GetAwaiter();
while (!results.IsCompleted)
;
var result = results.GetResult();
Console.WriteLine(result.Dump()); // prints "HELLO WORLD?"
}
Console.ReadKey();
}
}
}
There is a
LPRun7-x64.exeshipped with LinqPad (ARM64 and x86 versions are available as well) which allows to run LinqPad from a console. But you can also run it from a process.With the example below you could run your
samplequery.linqlike:Example:
Hello.linq:
Note that the example uses conditional compilation: Through
#if LINQPADstatements it can run directly inside of LinqPad itself, showing the output of the called Hello example in the result windows, or outside in a Visual Studio project.Details about the parameters for lprun can be found here. And if you just want to use LinqPad's extensions in the context of your VS project, then have a look there.