开发者

How can i look at query LINQ

How can i look at generated sql query by LINQ? I tried LINQPad but as i understood i have to write LINQ开发者_如何学编程 statements into LINQPad.

Any ways without SQL Profiler?

I use it here:

public IEnumerable<PeopleTalesCategory> GetAllCategories()
{
    return _dataContext.PeopleTalesCategories.OrderBy(c => c.PositionInMenu).ThenBy(c => c.NameAn);
}


If you are using Linq To Sql you can use a StringWriter to get the Sql generated by the Linq To Sql. Here is an example that setup to the DataContext the Log and execute a simple query to finally show in sqlLinqToSql.Text the SQL.

DataClasses1DataContext db = new DataClasses1DataContext();
StringWriter sw = new StringWriter();
db.Log = sw;

this.gridLinqToSql.DataSource = db.Customers.Where(c => c.CustomerID.StartsWith("A"));
this.gridLinqToSql.DataBind(); //Here is when the Linq query will be executed.

sqlLinqToSql.Text = sw.GetStringBuilder().ToString();

If you are using Linq To Entity, it's different. You need to call from the ObjectQuery the ToTraceString():

var cust = (from c in context.Customers select c);
string sql = ((ObjectQuery)cust).ToTraceString();


Hibernating Rhinos also has a tool that will do this for you (and then some: http://l2sprof.com/ There's also version for Entity Framework and NHibernate.


Just as a side note, custom enumerators such as IEnumerable Method() calls allow you to actually call "GetEnumerator()" method and return it. I have used such constructs (using your example) as "GetAllCategories().GetEnumerator()" and use it.

Perhaps you can use the "AsQueryable()" extension method within LINQPad to see something a bit more substantive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜