How to parse LINQ queries

I swear I saw how to do this before, but now that I really need to do this, I can’t remember where I saw it. I need two different things -

1) to see the actual SQL query that is generated by the LINQ query; and 2) when the SQL query actually gets into the database to perform any operations (CRUD)

Is there a tool that will allow me to do this?

EDIT

Sorry, should have given more details. - LINQ to Entities is what I use. - In addition, I do not have administrator rights on our instance of SQL Server, so I can not use SQL Profiler. I could always call the database administrator and get them to do this for me, but this is a hassle. I should have mentioned this, and I'm sorry. What I really want is a tool that I can use in my own window, which will allow me to see when the LINQ query gets into the database , when I'm in debug mode (debugging and code jump).

+4
source share
7 answers

Try using SQL Profiler . It's great to see what LINQ to SQL generates.

SQL Profiler is a graphical tool that allows system administrators to track events in an instance of Microsoft® SQL Server ™. You can record and save data about each event in a SQL Server file or table for further analysis. For example, you can monitor your production environment to see which stored procedures impede performance by running too slowly.

LINQPad is also a great tool for writing linq and sql statements for testing.

LINQPad compiles your queries using the .NET CSharpCodeProvider (or VBCodeProvider). Because C # and VB are statically typed, any database objects that you reference require support for typed DataContext. For performance, LINQPad builds typed DataContexts on the fly using Reflection.Emit instead of generating and compiling source code. It uses LINQ to SQL, not Entity Framework, because LINQ to SQL creates an order metamodel faster when instantiated.

+7
source

.ToString() in IQueryable for LINQ to SQL will show you the query.

 var myquery = from x in dbcontext.MyTable where x.Prop1 == someValue select new { value1 = x.prop1, value2 = 5, }; var sqlstring = myquery.ToString(); //<= look in this string 
+3
source

There are several tools that can help.

L2SProf (Linq2Sql Profiler) is a payment tool that can do a lot to help you see and optimize your requests. http://l2sprof.com/

LinqPad is a great linq writing tool for sql queries and viewing the SQL code that comes out. It also allows you to write C # code to experiment with things. This is a great code pad. There is a free version, but you get extra features if you pay. http://www.linqpad.net/

Otherwise, there are instructions for viewing the generated sql for free with the built-in material: http://msdn.microsoft.com/en-us/library/bb386961.aspx

+2
source

Using Visual Studio 2010, turn on IntelliTrace , and you will see every LINQ To SQL query, LINQ To Entities, and generally everything that ultimately uses ADO.NET.

+1
source

Are you looking for the DataContext.Log property? (I assume you are using LINQ to SQL.)

You can create a TextWriter that TextWriter stack trace and timestamp every time it was written, which will give you time information.

0
source
  • completely dependent on ORM. Most ORMs offer a kind of Log property that you can connect to TextWriter or so. Consult your ORM docs to find out more.
  • In general: when you use some SaveChanges method. This is due to (1), the registrar will provide you with information.
0
source

Source: https://habr.com/ru/post/1346275/


All Articles