Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate LINQ to sql statement

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()
like image 471
zielu1 Avatar asked Feb 15 '09 12:02

zielu1


People also ask

How LINQ queries converted into SQL queries?

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.

Is LINQ converted to SQL?

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.

How does LINQ to SQL work?

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.

How do I get LINQ to SQL in Visual Studio?

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.


1 Answers

CustomDataContext dc = new CustomDataContext();
IQueryable<Customer> query =
  from c in dc.Customer
  where c.Country == "UK"
  select c;
//
string command = dc.GetCommand(query).CommandText;
like image 147
Amy B Avatar answered Nov 16 '22 02:11

Amy B