-2

when we use linq to sql then few visualizer exist which show us what sql is generating by linq but when we query object by linq then is there any way out to have similar sql statement. as exmple

string[] names =
        new string[] { "Jon Skeet", "Marc Gravell", "tvanfosson", 
                       "cletus", "Greg Hewgill", "JaredPar" };

    var results = from name in names
                  where name.StartsWith("J")
                  select name;

can we extract sql from result?

a person advise to use this below function to have sql....

public static class MyExtensions
{
public static string ToTraceString<T>(this IQueryable<T> t)
{
    string sql = "";
    ObjectQuery<T> oqt = t as ObjectQuery<T>;
    if (oqt != null)
        sql = oqt.ToTraceString();
    return sql;
}
}

is it true? if yes can u tell me how to convert my result to IQueryable and then call ToTraceString() method. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

2

Linq to objects will not produce SQL for you. This would only be the case if you were using linq to entities or linq to sql.

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
2

I am pretty sure that you cannot use SQL against 'linq2objects', so getting SQL from those is not possible.

Linq 2 objects works, basically, as table-scan in sql. When executed, it reads lines of source one by one, and applies filters/selectors on each of them. See, for exmaple, http://blogs.msdn.com/b/wesdyer/archive/2007/01/03/how-linq-to-objects-queries-work.aspx.

I assume you are interested in debugging query, and there is another topic on stack overflow answering this: How to debug a LINQ Statement

Community
  • 1
  • 1
nothrow
  • 15,882
  • 9
  • 57
  • 104