我的问题是这个查询之间的速度是否存在差异
query.Where(i => (i.FirstName.Contains(firstname) || string.IsNullOrEmpty(firstname)) &&
(i.LastName.Contains(secondname) || string.IsNullOrEmpty(secondname)) &&
(i.DateOfOrder == date || date == default(DateTime)));
和这段代码:
if (!String.IsNullOrEmpty(firstname))
{
query = query.Where(i => i.FirstName.Contains(firstname));
}
if (!String.IsNullOrEmpty(secondname))
{
query = query.Where(i => i.FirstName.Contains(firstname));
}
if (date!=default(DateTime))
{
query = query.Where(i => i.DateOfOrder==date);
}
编辑,谢谢你的答案。
答案 0 :(得分:1)
我猜第二段代码应如下所示:
if (!String.IsNullOrEmpty(firstname))
{
query = query.Where(i => i.FirstName.Contains(firstname));
}
if (!String.IsNullOrEmpty(secondname))
{
query = query.Where(i => i.FirstName.Contains(firstname));
}
if (date!=default(DateTime))
{
query = query.Where(i => i.DateOfOrder==date);
}
但无论如何,你可以检查生成的最终sql查询并改为比较它们。
获取sql查询,你可以
通过调用dbCtx.Log = Console.Out
对您的SQL Server进行配置,并查看有哪些查询