Get SQL string from Linqkit
<p>how can I return the SQL from a <code>LinqKit.ExpandableQuery&lt;T&gt;</code>?<br>
I'm using the code from <a href="https://www.stevefenton.co.uk/2015/07/getting-the-sql-query-from-an-entity-framework-iqueryable/" rel="nofollow" title="stevefenton.co.uk">stevefenton.co.uk</a> but it seems to fail at</p>

<pre><code>private static System.Data.Entity.Core.Objects.ObjectQuery&lt;T&gt; GetQueryFromQueryable&lt;T&gt;(IQueryable&lt;T&gt; query)
        {
            var internalQueryField = query.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Where(f =&gt; f.Name.Equals("_internalQuery")).FirstOrDefault();
            var internalQuery = internalQueryField.GetValue(query);
            var objectQueryField = internalQuery.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Where(f =&gt; f.Name.Equals("_objectQuery")).FirstOrDefault();
            return objectQueryField.GetValue(internalQuery) as System.Data.Entity.Core.Objects.ObjectQuery&lt;T&gt;;
        }
</code></pre>

<p>with the <code>internalQueryField</code> being null.</p>

<p>It has something to do with the inheritance tree (LinqKit + Linq), but I can't
get my head around it.. :(</p>