Home

How to run a LinqPad script in a C # console application

I want to run some LinqPad scripts in a C # console application.

I downloaded the LinqPad package from nuget. And I wrote a piece of code like this:
LINQPad.ObjectModel.QueryCompilation compiledScript = LINQPad.Util.Compile("C:\\temp\\testSkryptu.linq"); LINQPad.ObjectModel.QueryExecuter exec = compiledScript.Run(LINQPad.QueryResultFormat.Text); await exec.WaitAsync(); var rr = exec.AsString();

Each of the scripts that are run returns a list of data to the console application. The above code, however, does not return this data to me as a collection, but I can extract it as a string.
Sample LinqPad script looks like this:
`List Main()
{
using (var connection = new SqlConnection("Data Source=.;Initial Catalog=sezam;Integrated Security=SSPI; Application Name=SezamServicehost_DEV;TImeout=9999;"))
{
connection.Open();

    var sql =
        [email protected]"select m.ProjectNumber, m.VipCustomerNumber, m.Id as MessageId, m.OrderCount as MessageQuantity, ISNULL(p.ErpCode, '-') as ErpCode, ISNULL(s.Name, '-') as Name, 
            ISNULL(o.OrderNumber, '-') as OrderNumber, ISNULL(i.Quantity, 0) as Quantity, ISNULL(i.Price, 0) as Price, ISNULL(u.Shortcut, '-') as PM, ISNULL(o.ErpOrderStatus, '-') as ErpOrderStatus,
            u2.Shortcut as POS, m.OrderDate as Dedline, o.CreationDate, o.OrderDate,               
            case
              when m.OrderAnswerOrderIsRejected = 1 Then (case when o.OrderNumber is not null Then 'Błąd' else 'Odrzucone' end)
              when (m.AnswerDate is null and o.OrderNumber is null) or (o.ErpOrderStatus = 2 or ErpOrderStatus = 3) Then 'Nowe'
              when (m.AnswerDate is null and o.OrderNumber is not null) Then 'Błąd'                
              when (m.AnswerDate is not null and o.OrderNumber is not null) and (o.ErpOrderStatus = 0 or o.ErpOrderStatus = 1) Then  (case when i.Quantity < m.OrderCount Then 'Możliwy brak' else 'Zamówione' end)
              when (m.AnswerDate is not null and o.OrderNumber is null) Then 'Potwierdzone - nie zamówione'                  
              when (m.AnswerDate is not null and o.OrderNumber is not null and o.ErpOrderStatus is null) Then 'Potwierdzone - nie zamówione'
              when (m.AnswerDate is not null and o.OrderNumber is not null) and ErpOrderStatus = 4 and (w.Quantity - w.AllocatedQuantity) >= 0 Then 'Zamknięte'
              when ((m.AnswerDate is not null and o.OrderNumber is not null) and ErpOrderStatus = 4 and (w.Quantity - w.AllocatedQuantity) < 0) or (i.Quantity < m.OrderCount) Then 'Możliwy brak'
            end as Status
            from DbMessage m 
              left join DbSupplierOrderItem i on ISNULL(m.ProjectNumber, '') = ISNULL(i.ProjectNumber, '') and ISNULL(m.VipCustomerNumber, '') = ISNULL(i.VipCustomerNumber, '') and m.Product = i.Product
              left join DbProduct p on m.Product = p.Id
              left join DbSupplierOrder o on i.SupplierOrder = o.Id
              left join DbUser u on o.Owner = u.Id
              left join DbSupplier s on o.Supplier = s.Id
              left join DbWarehouseStock w on p.Id = w.Product and w.Warehouse = 8093696
              left join DbUser u2 on m.Sender = u2.Id
            where (m.ProjectNumber is not null and m.ProjectNumber != '') or (m.VipCustomerNumber is not null and m.VipCustomerNumber != '')
            order by m.ProjectNumber, m.VipCustomerNumber";

    var data = connection.Query(sql).ToList();
    return data;
}

}`

What the script and C # code should look like so that I can get the list of objects from the scripts and not the string to the application

Comments

Sign In or Register to comment.