Asp.net mvc LINQ到SQL:制作一个;双入式;查询崩溃
我需要做以下事情:Asp.net mvc LINQ到SQL:制作一个;双入式;查询崩溃,asp.net-mvc,linq,linq-to-sql,Asp.net Mvc,Linq,Linq To Sql,我需要做以下事情: var a = from c in DB.Customers where (from t1 in DB.Table1 where t1.Date >= DataTime.Now select t1.ID).Contains(c.ID) && (from t2 in DB.Table2 where t2.Date >= DataTime.Now
var a = from c in DB.Customers
where (from t1 in DB.Table1 where t1.Date >= DataTime.Now
select t1.ID).Contains(c.ID) &&
(from t2 in DB.Table2 where t2.Date >= DataTime.Now
select t2.ID).Contains(c.ID)
select c
它不想跑。我得到以下错误:
超时已过期。超时时间
在完成测试之前经过
操作或服务器不可用
回应
但当我尝试跑步时:
var a = from c in DB.Customers
where (from t1 in DB.Table1 where t1.Date >= DataTime.Now
select t1.ID).Contains(c.ID)
select c
或:
它起作用了!我确信这两个查询中都包含一些客户ID。它不一定会崩溃,但很可能会生成一个效率低下的查询,并且超时。一个好办法是运行SQL Server探查器,查看SQL中发出的实际查询,然后对此进行一些分析。如果这是一个效率问题,最好查看LINQ to SQL生成的SQL查询(在调试模式下,将鼠标光标放在
A
上)。在任何情况下,您都可以尝试使用join
重写查询。像这样的事情应该可以做到:
var a = from c in DB.Customers
join t1 in DB.Table1 on c.ID equals t1.ID
join t2 in DB.Table2 on c.ID equals t2.ID
where t1.Date >= DateTimeNow && t2.Date >= DateTimeNow
select c
我发现了问题。它在我的NEWID()order by方法中,因为我想得到随机结果。当我移除它时,它工作正常。如何使用NEWID()?我不知道如何将LINQ调试成SQL!非常感谢。它会创建EXISTS查询,并且再次占用太多的负载。如果我只使用一个存在,它就工作了。我将尝试查找问题所在,或使用联接查询。有没有办法在查询中使用它而不是现有的查询?我发现了这个问题。它在我的NEWID()order by方法中,因为我想得到随机结果。当我移除它时,它工作正常。如何使用NEWID()?
var a = from c in DB.Customers
join t1 in DB.Table1 on c.ID equals t1.ID
join t2 in DB.Table2 on c.ID equals t2.ID
where t1.Date >= DateTimeNow && t2.Date >= DateTimeNow
select c