Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqPad - Convert SQL to Linq command

Tags:

linqpad

I recently purchased LINQPad in hopes that it would allow me to convert SQL statements into LINQ statements.

Using LINQPad, I am able to attach a DB and run the SQL statement which returns the results I need.

But I can not find a 'command' to convert that SQL statement into LINQ.

Can you please let me know how to convert SQL to LINQ by using LINQPad OR another tool?

like image 514
Ravi Ram Avatar asked Sep 02 '12 18:09

Ravi Ram


People also ask

How can I convert SQL query to LINQ using LINQPad?

Click on "Add connection"; a window will appear. Choose "Default (LINQ to SQL)" and click on the "Next" button. A new window will appear, fill in the required details to get connected with the desired database.

What is LINQPad used for?

LINQPad is a software utility targeted at . NET Framework and . NET Core development. It is used to interactively query SQL databases (among other data sources such as OData or WCF Data Services) using LINQ, as well as interactively writing C# code without the need for an IDE.

What is LINQ to SQL?

LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Note. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.


2 Answers

There is a tool called Linqer, but be careful: transliterating from SQL to LINQ can give you the worst of both worlds.

For instance, suppose you want all purchases of $1000 or greater paid for in cash or by customers who live in Washington. Here's the query in SQL:

SELECT p.*
FROM Purchase p
    LEFT OUTER JOIN 
        Customer c INNER JOIN Address a ON c.AddressID = a.ID
    ON p.CustomerID = c.ID  
WHERE
   (a.State = 'WA' || p.CustomerID IS NULL)
    AND p.ID in
    (
        SELECT PurchaseID FROM PurchaseItem
        GROUP BY PurchaseID HAVING SUM (SaleAmount) > 1000
    )

How would translate this to LINQ? The wrong way is to transliterate the query into LINQ, trying to reproduce the outer and inner joins, subquery and group clause. The right way is to map your original query (in English) directly into LINQ, leveraging LINQ's linear flow of data and association properties:

I want all purchases...

from p in db.Purchases

...of $1000 or greater...

where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000

...paid for in cash...

where p.Customer == null

...or by customers who live in Washington

|| p.Customer.Address.State == "WA"

Here's the final query:

from p in db.Purchases
where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000
where p.Customer == null || p.Customer.Address.State == "WA"
select p

More info here.

like image 123
Joe Albahari Avatar answered Nov 04 '22 00:11

Joe Albahari


In general there are no tools to covert SQL to Linq as @andres-abel mention before, but sometimes you have to write Linq that will execute exactly as specified SQL (for example because of performance issues, backward compatability or some other reasons).

In this case I'll advice you to do reverse engineering by yourself:

  1. configure logging of dump SQL statements generated by Linq to stdout using
    • ObjectQuery.ToTraceString,
    • DbCommand.CommandText,
    • logger availabe to your data source
  2. manually rewrite Linq statement until you'll get what you need
like image 26
Akim Avatar answered Nov 03 '22 23:11

Akim