Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trace the query produced by the documentdb Linq provider?

How can I see the document DB sql query (in a string) generated by a linq statement before it gets sent to the server?

_documentClient.CreateDocumentQuery<MyType>(
                        UriFactory.CreateDocumentCollectionUri(DatabaseName,
                            CollectionName)).Where(....).SelectMany(...)

I want to use this for tracing purposes.

like image 849
Michiel Cornille Avatar asked Dec 07 '16 17:12

Michiel Cornille


People also ask

How do I get SQL query from LINQ while debugging?

You can get same generated SQL query manually by calling ToString: string sql = committeeMember. ToString(); This overridden method internally calls ObjectQuery.

On which data sources do LINQ queries work?

In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, . NET collections, and any other format for which a LINQ provider is available.

What is LINQ provider?

A linq provider is software that implements the IQueryProvider and IQueryable interfaces for a particular data store. In other words, it allows you to write Linq queries against that data store. For example, the Linq to XML provider allows you to write Linq queries against XML documents.


2 Answers

You can call ToString() on the DocumentDB query to get the SQL translation of the LINQ expression that's sent over the wire.

string sql = client.CreateDocumentQuery<MyType>(collectionUri).Where(t => t.Name = "x").ToString();
// sql is somthing like SELECT * FROM c WHERE c["Name"] = "x"
like image 135
Aravind Krishna R. Avatar answered Sep 19 '22 00:09

Aravind Krishna R.


You could just use Fiddler and view the JSON of the request after it is sent.

View an example here:

CosmosDB\DocumentDB Generated SQL Query

like image 44
Adam Hockemeyer Avatar answered Sep 19 '22 00:09

Adam Hockemeyer