这是我在C#中的LINQ查询:
var q =
from a in context.tableA.Where(s => s.field1 == id) //I'm querying by "id"
from b in context.tableB.Where(s => s.field1 == a.field1)
from c in context.tableC.Where(s => s.field2 == b.field2).DefaultIfEmpty() //left join
from d in context.tableD.Where(s => s.field3 == b.field3).DefaultIfEmpty()
from e in context.tableE.Where(s => s.field4 == b.field4).DefaultIfEmpty()
select new CustomObject()
{
CustomObject_fieldA = a.someField,
CustomObject_fieldB = b.someField,
//some other fields assigned...
CustomObject_fieldC = c.someField,
CustomObject_fieldD = d.someField,
CustomObject_fieldE = e.someField // <-- THIS LINE
}
因此,为了澄清,我的表格结构如下:
tableA: int field1
tableB: int field1 int field2 int field3 int field4
tableC: int field2
tableD: int field3
tableE: int field4
我的问题
如果我发表评论THIS LINE
,那么查询将会成功。
如果我将THIS LINE
取消注释,则查询将会挂起,并显示EntityCommandExecutionException
错误(Timeout expired
错误)。
由于我正在进行左连接,因此我知道e
可能是NULL
。但我做了一些测试,告诉我这不会导致错误。
- TEST 1 -
首先,注释掉THIS LINE
。然后,c
和d
可能是NULL
,也可能不是c
,无论d
还是NULL
是THIS LINE
,查询都会成功。
- 测试2 -
取消注释e
。 NULL
是否为THIS LINE
,查询将始终失败(挂起上面给出的错误)。
问题
我已经进行了大量测试并根据// in the CustomObject.cs file, some of the properties are
public string CustomObject_fieldA { get; set; }
public string CustomObject_fieldB { get; set; }
public string CustomObject_fieldC { get; set; }
public string CustomObject_fieldD { get; set; }
public string CustomObject_fieldE { get; set; }
的评论或取消注释来缩小成功/失败(挂起)。
有谁知道为什么会这样,以及如何解决?
编辑以澄清
{{1}}
答案 0 :(得分:0)
由于您在执行LINQ查询时遇到Timeout错误,我的建议是为您的LINQ查询生成SQL,然后通过分析器运行它以查看查询优化器正在执行的操作。
var sql = ((System.Data.Objects.ObjectQuery)q).ToTraceString();
如果您正在使用大型数据集,则可能会从未优化的连接或可能只是资源不足的数据库服务器发生超时。无论哪种方式,探查器都应该告诉你超时发生了什么。