如何查看我的lambda表达式的SQL查询

时间:2013-11-13 01:30:57

标签: c# linq lambda

    public static IList<EmployeeObject> GetAllEmployee()
    {
        IList<EmployeeObject> emps = new List<EmployeeObject>();

        using (SqlConnection conn = new SqlConnection(connString))
        {
            connection.Open();
            //....
            using (SqlDataReader reader = sqlCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                  //...add
                }
            }
         }
       return emps.ToList();
     }


public static IQueryable<EmployeeObject> QueryableSQL()
{
    IQueryable<EmployeeObject> queryable = EmployeeRepository.GetAllEmployee(); 
}

交叉检查Lambda表达式查询的要求是否正确。这意味着lambda表达式如何执行,我没有使用Entity框架,我在网上搜索过,我没有找到任何与我正在寻找的相关的博客。 任何帮助SQL事件探查器?还是ObjectQuery?任何能告诉我sql正在生成什么的东西。

var query = QueryableSQL().Where(employee => 
    employee.Interests.Any(interest => interest.Name ==  "Chess"));

1 个答案:

答案 0 :(得分:1)

如果您使用的是LINQ to SQL,则可以执行以下操作

  1. 实际上,您可以做的最简单的事情是查看ToString的{​​{1}}方法:

    IQueryable
  2. 您可以使用Linq IQueryable<Employee> query = ...; //your query var querySql = query.ToString(); 的{​​{1}}属性:

    Log

  3. 在EF中,您可以使用DataContext的{​​{1}}方法:

    using (var dc = new TestDataContext()) //your DataContext
    {
      dc.Log = Console.Out; //it's of TextWriter type, use any suitable
      IQueryable<Employee> query = ...; //your query
    
      //execute the query, as it'll output the query to the Log only after it's
      //really executed on the database
      foreach (var s in query)
      {
          //...
      }
    }
    

    或使用Visual Studio Intelli Trace ...获取更多信息,请阅读:Profiling Database Activity in the Entity Framework


    现在可以看到,它只是对象的LINQ ......所以LINQ根本不查询数据库。它与内存中的对象一起运行。那么,Visual Studio缺少LINQ的任何查询分析器。您可以尝试使用LINQPad来测试LINQ查询: www.linqpad.net/