3

how can I return the SQL from a LinqKit.ExpandableQuery<T>?
I'm using the code from stevefenton.co.uk but it seems to fail at

private static System.Data.Entity.Core.Objects.ObjectQuery<T> GetQueryFromQueryable<T>(IQueryable<T> query)
        {
            var internalQueryField = query.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Where(f => 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 => f.Name.Equals("_objectQuery")).FirstOrDefault();
            return objectQueryField.GetValue(internalQuery) as System.Data.Entity.Core.Objects.ObjectQuery<T>;
        }

with the internalQueryField being null.

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

1 Answer 1

4
string sql = query.ToString();
Sign up to request clarification or add additional context in comments.

3 Comments

when a programmer brain hangs he/she would do strange things, I have done it many times, no worry ;)
i was so convinced it would not work that I didn't even try :)
This does not seem to work in .Net core 2.2. You just get a reflection string i.e. "value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[my.object.typehere])"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.