我需要做以下事情:
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
或者:
var a = from c in DB.Customers
where (from t2 in DB.Table2 where t2.Date >= DataTime.Now
select t2.ID).Contains(c.ID)
select c
有效!我确信两个IN查询都包含一些客户ID。
答案 0 :(得分:3)
如果这是一个效率问题,最好查看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
答案 1 :(得分:1)
它不一定会崩溃,而是可能产生一个超时的低效查询。一个好处是运行SQL Server Profiler以查看在SQL中发出的实际查询,然后对其进行一些分析。
答案 2 :(得分:0)
我发现了问题。这是我的NEWID()顺序方法,因为我想获得随机结果。当我删除它,它工作正常。我该如何使用NEWID()?