<p>I am currently using Linq to NHibernate (although that is not an issue with regards to this question) to execute queries against my database and I want to be able to test whether the current <code>IQueryable</code> result instance has been executed or not.</p>

<p>The debugger knows that my <code>IQueryable</code> has not been <em>'invoked'</em> because it tells me that expanding the Results property will <em>'enumerate'</em> it. Is there a way for me to programmatically identify that as well.</p>

<p>I hope that makes sense :)</p>

[{'answer_id': 55848, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 0, 'selected': False, 'text': '<p>I believe you can use <a href="https://learn.microsoft.com/en-us/dotnet/api/system.data.linq.datacontext.log" rel="nofollow noreferrer">DataContext.Log</a> to log everything that is executed.</p>\n'}, {'answer_id': 81740, 'author': 'Mark Pattison', 'author_id': 15519, 'author_profile': 'https://Stackoverflow.com/users/15519', 'pm_score': 0, 'selected': False, 'text': "<p>Assuming you're using Visual Studio, you can insert <code>DataContext.Log = Console.Out</code> into your code.  You can then watch the SQL as it's executed, in the output window.</p>\n\n<p>I'm not sure whether it's possible to programatically test whether the query has been executed.  You can force it to execute, for example by calling <code>.ToList</code> on the query.</p>\n"}, {'answer_id': 85732, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 3, 'selected': True, 'text': '<p>How about writing an IQueryable wrapper like this:</p>\n\n<pre><code>class QueryableWrapper&lt;T&gt; : IQueryable&lt;T&gt;\n{\n    private IQueryable&lt;T&gt; _InnerQueryable;\n    private bool _HasExecuted;\n\n    public QueryableWrapper(IQueryable&lt;T&gt; innerQueryable)\n    {\n        _InnerQueryable = innerQueryable;\n    }\n\n    public bool HasExecuted\n    {\n        get\n        {\n            return _HasExecuted;\n        }\n    }\n\n    public IEnumerator&lt;T&gt; GetEnumerator()\n    {\n        _HasExecuted = true;\n\n        return _InnerQueryable.GetEnumerator();\n    }\n\n    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n\n    public Type ElementType\n    {\n        get { return _InnerQueryable.ElementType; }\n    }\n\n    public System.Linq.Expressions.Expression Expression\n    {\n        get { return _InnerQueryable.Expression; }\n    }\n\n    public IQueryProvider Provider\n    {\n        get { return _InnerQueryable.Provider; }\n    }\n}\n</code></pre>\n\n<p>Then you can use it like this:</p>\n\n<pre><code>var query = new QueryableWrapper&lt;string&gt;(\n    from str in myDataSource\n    select str);\n\nDebug.WriteLine("HasExecuted: " + query.HasExecuted.ToString());\n\nforeach (string str in query)\n{\n    Debug.WriteLine(str);\n}\n\nDebug.WriteLine("HasExecuted: " + query.HasExecuted.ToString());\n</code></pre>\n\n<p>Output is:</p>\n\n<p>False<br/>\nString0<br/>\nString1<br/>\n...<br/>\nTrue</p>\n'}]
['https://Stackoverflow.com/questions/55843', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4884/']