I'm attempting to capture query results from linq expressions sent to a DataContext (Devart.Data.Linq). I am able to record the queries by making use of the Log property but have not yet found a way to intercept the IQueryable results.
The intention is to pair the queries and results together, serialize them, and use them later for mocking the data context in a test suite.
All of the Devart.Data.Linq.DataContext methods that look promising (ExecuteQuery, Query) are non-virtual and so I can't override them to store the results. I don't want to record the results at the point in which the linq queries are written as it will be very messy and mean pasting in test related code throughout the application.
I eventually found a roundabout way of intercepting both queries and results through wrapping both the DataContext and the contained IQueryables. This required implementing a custom IQueryable and IQueryProvider. A good example is given at https://blogs.msdn.microsoft.com/alexj/2010/03/01/tip-55-how-to-extend-an-iqueryable-by-wrapping-it/
Once that was done it was possible to catch the results of queries in the overriden IQueryProvider.Execute
method. Catching the associated SQL generated by the ORM required tracking the DataContext.Log
property for changes before and after the query was executed.