Sunday, July 12, 2009

Debuging ObjectQuery

When you write query with ObjectQuery, it use IQueryable interface. Following extension function help your to debug ObjectQuery more easily.

public static class IQueryableExtenstion { public static ObjectQuery ToObjectQuery(this IQueryable query) { return query as ObjectQuery; } public static ObjectQuery<T> ToObjectQuery<T>(this IQueryable<T> query) { return query as ObjectQuery<T>; } public static string ToDatabaseSql(this IQueryable query) { try { return query.ToObjectQuery().ToTraceString(); } catch { return null; } } public static string ToEntitySql(this IQueryable query) { try { return query.ToObjectQuery().CommandText; } catch { return null; } } public static void OuputTrace(this IQueryable query) { Console.WriteLine(query.ToDatabaseSql()); Console.WriteLine(query.ToEntitySql()); } } //to use the extension function you can write the following code var test = from a in context.Addresses let c = new { a.Contact.FirstName, a.Contact.LastName, a.CountryRegion } group c by c.CountryRegion into mygroup where (mygroup.Count() > 150) select mygroup; test.OuputTrace();

No comments:

Post a Comment