سؤال

I've got a basic linq query that I want to put to a CSV file. The following code works fine when run from LinqPad:

var phxTix = Companies
.Select (c => new { 
    Ticker = c.Ticker.Trim(),
    FameTick = c.Coverage_Status.Trim()});

Util.WriteCsv(phxTix, @"c:\Temp\phxticks.csv");

But I get an error when I run the following Powershell script from the command line:

$srcLoc = "c:\linqscripts\GetPhxTix.linq"
$destLoc = "c:\Temp\phxticks.csv"
lprun -lang=e -cxname=myDB -format=csv $srcLoc > $destLoc

The error I get is:

lprun : c:\Users\AppData\Local\Temp\LINQPad\_ibakcumi\query_bggevp.cs(35,36) : error CS1513: } 
expected
At c:\PS\GetPhxTicksJob.ps1:4 char:1
+ lprun -lang=e -cxname=myDB $srcLoc > $destLoc
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (c:\Users\...513: } expected:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

c:\linqscripts\GetPhxTix.linq(31,3) : error CS1519: Invalid token ')' in class, struct, or 
interface member declaration
c:\linqscripts\GetPhxTix.linq(33,1) : error CS1022: Type or namespace definition, or end-of-file expected

What am I doing wrong? Or what am I missing to execute this cli script?

هل كانت مفيدة؟

المحلول

Chris is correct: start by omitting the -lang switch. You also don't need to specify a connection unless you want to change it from what's stored in the .linq script.

There's another problem: your query is writing data to a CSV file and you are also telling lprun to write the output to the same file (which will be null, since it doesn't dump anything). So you'll end up overwriting the CSV file with a blank one.

To fix this, you need to decide between one approach and the other. Either call Util.WriteCsv in the LINQ file itself (as you're doing), and call lprun without redirecting the output, or remove the call to Util.WriteCsv from your query and change the query language to "C# Expression". If you do the latter, your query should read like this:

Companies
.Select (c => new { 
    Ticker = c.Ticker.Trim(),
    FameTick = c.Coverage_Status.Trim()})

and your call to lprun should read like this:

lprun -format=csv $srcLoc > $destLoc

نصائح أخرى

I think your problem is that you're explicitly telling LinqPad to execute that file as an "Expression" (the -lang=e flag). I suspect it would work if you used -lang=s (Statement).

Worth noting that since you're running a .linq file, you may not you need to specify the lang flag at all (the lang option is often handy to use for running plain txt files).

If you open up the .linq file in a text editor, if you saved the script in LinqPad, you should see something like this at the top: <Query Kind="Statements" /> which LinqPad will use to determine how to run the script (unless you override it).

Further info: http://www.linqpad.net/lprun.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top