PDA

View Full Version : حرفه ای: بدست آوردن کوئری sql تولید شده توسط linq



hmahdavi921
سه شنبه 27 تیر 1396, 13:46 عصر
سلام
آیا امکان داره کوئری sql کوئری های لینکی که مینویسیم رو بدست بیاریم ؟
مثلا :
List<BusProvince> provinces;
provinces = (from u in db.BusProvinces
where (from s in db.BusProvinces
join buscities in db.BusCities on s.Id equals buscities.ProvinceId
join buspath in db.BusPaths on buscities.Id equals buspath.DepId
select s.Id).Contains(u.Id)
select u).ToList();

با تشکر

Mohammadv2
جمعه 06 مرداد 1396, 04:39 صبح
https://weblogs.asp.net/scottgu/linq-to-sql-debug-visualizer

hmahdavi921
شنبه 14 مرداد 1396, 15:04 عصر
پولیه .

ali_md110
شنبه 14 مرداد 1396, 21:22 عصر
برنامه های پروفایلر زیاد هست
ولی میتونید از کلاس زیر هم استفاده کنید


using System;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Text;

namespace Common.EFToolkit
{
/// <summary>
/// این کلاس خروجی دستورات ای اف را به اسکیول نشان میدهد
/// <sample>
/// کلاس فوق را کافی است تنها یکبار در آغاز برنامه Application_Start :
///DbInterception.Add(new EfCommandInterception());

/// </sample>
/// </summary>
public class EfCommandInterception : DbCommandInterceptor
{
public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
var timespan = runCommand(() => base.ScalarExecuting(command, interceptionContext));
logData(command, interceptionContext.Exception, timespan);
}

public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
var timespan = runCommand(() => base.NonQueryExecuting(command, interceptionContext));
logData(command, interceptionContext.Exception, timespan);
}

public override void ReaderExecuting(DbCommand command,
DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
var timespan = runCommand(() => base.ReaderExecuting(command, interceptionContext));
logData(command, interceptionContext.Exception, timespan);
}

private static Stopwatch runCommand(Action command)
{
var timespan = Stopwatch.StartNew();
command();
timespan.Stop();
return timespan;
}

private static void logData(DbCommand command, Exception exception, Stopwatch timespan)
{
if (exception != null)
{
Trace.TraceError(formatException(exception, "Error executing command: {0}", command.CommandText));
}
else
{
Trace.TraceInformation(string.Concat("Elapsed time: ", timespan.Elapsed, " Command: ",
command.CommandText));
}
}

private static string formatException(Exception exception, string fmt, params object[] vars)
{
var sb = new StringBuilder();
sb.Append(string.Format(fmt, vars));
sb.Append(" Exception: ");
sb.Append(exception.ToString());
while (exception.InnerException != null)
{
sb.Append(" Inner exception: ");
sb.Append(exception.InnerException.ToString());
exception = exception.InnerException;
}
return sb.ToString();
}
}
}




سپس بعد از اجرای برنامه
Output از منوی View ویژوال استودیو انتخاب کنبد و کوئریها رو ببیندیدش