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 Framework ,我在网上搜索过,但没有找到任何与我要查找的内容相关的博客。 SQL Profiler 有帮助吗?或者对象查询?任何显示 sql 生成内容的内容。
var query = QueryableSQL().Where(employee =>
employee.Interests.Any(interest => interest.Name == "Chess"));
最佳答案
如果您使用 LINQ to SQL,则可以执行以下操作:
实际上,您可以做的最简单的事情就是查看
IQueryable
的ToString
方法:IQueryable<Employee> query = ...; //your query var querySql = query.ToString();
您可以使用 Linq
DataContext
的Log
属性: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) { //... } }
在 EF 中,您可以使用 ObjectQuery
的 ToTraceString
方法:
using (var dc = new TestEntities())
{
var query = ...;// your query
var oquery = query as System.Data.Objects.ObjectQuery;
Console.WriteLine(oquery.ToTraceString());
}
或者使用 Visual Studio Intelli Trace...有关详细信息,请阅读:Profiling Database Activity in the Entity Framework
正如现在所见,它只是 LINQ to 对象...所以 LINQ 根本不查询数据库。它对内存中的对象进行操作。嗯,Visual Studio 缺少任何用于 LINQ 的查询分析器。您可以尝试使用 LINQPad 来测试您的 LINQ 查询: www.linqpad.net/
关于c# - 如何查看 lambda 表达式的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19943547/