如何查看linq生成的sql
类别:编程学习 浏览量:574
时间:2014-11-27 如何查看linq生成的sql
如何查看linq生成的sql一、控制台程序中
添加:db.Log = Console.Out;
二、写入日志文件
StreamWriter sw = new StreamWriter("F:\\\\Linqlog.txt", true);
db.Log = sw;
......//Linq to Sql代码
sw.Close();
三、利用ObjectQuery的ToTraceString查看生成的SQL语句:
public IQueryable<T_Bas_PlanInfo> GetT_Bas_PlanInfoByCondition(string industryId)
{
var list = this.ObjectContext.T_Bas_EnterpriseProductionData
.Where(e => e.region.Equals(industryId))
.Select(e => e.latitude.Trim())
.Distinct();
var results = this.ObjectContext.T_Bas_PlanInfo
.Where(e => list.Any(e2 => e.plangeneral.Contains(e2)));
var sql = (results as ObjectQuery<T_Bas_PlanInfo>).ToTraceString();
return results;
}
四、利用GetCommand方法
dataContext.GetCommand(query).CommandText;
五、使用LINQPad
LINQPad支持 LINQ to SQL、LINQ to Objects、LINQ to XML等等
六、LINQ to SQL Debug Visualizer
ScottGu的LINQ to SQL Debug Visualizer可以在Debug过程中查看SQL语句.
七、DebuggerWriter工具类
可以选择将Log信息直接发送到Debug的输出窗口.
DebuggerWriter源码
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
namespace Vandermotten.Diagnostics
{
/**//// <summary>
/// Implements a <see cref="TextWriter"/> for writing information to the debugger log.
/// </summary>
/// <seealso cref="Debugger.Log"/>
public class DebuggerWriter : TextWriter
{
private bool isOpen;
private static UnicodeEncoding encoding;
private readonly int level;
private readonly string category;
/**//// <summary>
/// Initializes a new instance of the <see cref="DebuggerWriter"/> class.
/// </summary>
public DebuggerWriter()
: this(0, Debugger.DefaultCategory)
{
}
/**//// <summary>
/// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level and category.
/// </summary>
/// <param name="level">A description of the importance of the messages.</param>
/// <param name="category">The category of the messages.</param>
public DebuggerWriter(int level, string category)
: this(level, category, CultureInfo.CurrentCulture)
{
}
/**//// <summary>
/// Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level, category and format provider.
/// </summary>
/// <param name="level">A description of the importance of the messages.</param>
/// <param name="category">The category of the messages.</param>
/// <param name="formatProvider">An <see cref="IFormatProvider"/> object that controls formatting.</param>
public DebuggerWriter(int level, string category, IFormatProvider formatProvider)
: base(formatProvider)
{
this.level = level;
this.category = category;
this.isOpen = true;
}
protected override void Dispose(bool disposing)
{
isOpen = false;
base.Dispose(disposing);
}
public override void Write(char value)
{
if (!isOpen)
{
throw new ObjectDisposedException(null);
}
Debugger.Log(level, category, value.ToString());
}
public override void Write(string value)
{
if (!isOpen)
{
throw new ObjectDisposedException(null);
}
if (value != null)
{
Debugger.Log(level, category, value);
}
}
public override void Write(char[] buffer, int index, int count)
{
if (!isOpen)
{
throw new ObjectDisposedException(null);
}
if (buffer == null || index < 0 || count < 0 || buffer.Length - index < count)
{
base.Write(buffer, index, count); // delegate throw exception to base class
}
Debugger.Log(level, category, new string(buffer, index, count));
}
public override Encoding Encoding
{
get
{
if (encoding == null)
{
encoding = new UnicodeEncoding(false, false);
}
return encoding;
}
}
public int Level
{
get { return level; }
标签:linq
您可能感兴趣
- 如何查看linq生成的sql
- Linq中where查询
- Linq中的TakeWhile和SkipWhile
- linq to sql 中Concat、Union、Intersect、Except
- linq中let
- linq 数据类型转换
- linq 排序
- Linq中select查询
- linq中group by
- linq xml 查询
- 使用 LINQPad 调试linq以及lambda表达式
- Linq操作Datable
- LINQ TO SQL 中join
- linq中AsEnumerable和AsQueryable的区别
- linq not in 查询
- LINQ中Aggregate的用法
- 如何追女孩子(如何追女孩子的技巧和方法)
- 是不是快乐全被你拿走了(而是你得到的)
- 世界上只有妈妈好(世界上只有妈妈好的歌词)
- 为什么现在社会越来越卷了(现在社会为什么发展那么快呢)
- 直播带货能赚到很多钱吗(直播带货能赚到很多钱吗现在)
- 做网红真的很能赚钱吗(做网红真的很能赚钱吗)