I want to translate LINQ expression tree to SQL statement and I don't want to write my own code for this.
Example:
var query = from c in Customers
where c.Country == "UK" &&
c.City == "London"
select c);
To
SELECT ... FROM Customers AS c WHERE c.Country = "UK" AND c.City = "London"
I know DataContext.Log
, but I want to use:
query.ToSqlStatementString()
LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.
LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a component of version 3.5 of the . NET Framework and ably does the translation of language-integrated queries of the object model into SQL. These queries are then sent to the database for the purpose of execution.
In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution.
To install the LINQ to SQL tools, start the Visual Studio installer, choose Modify, then select the Individual Components tab, and then select LINQ to SQL tools under the Code Tools category.
CustomDataContext dc = new CustomDataContext();
IQueryable<Customer> query =
from c in dc.Customer
where c.Country == "UK"
select c;
//
string command = dc.GetCommand(query).CommandText;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With