Binary = null 的 LINQ-to-SQL 枚举表达式失败
这是一个奇怪的 LINQ-to-SQL 问题,无法评估为 Enumerable(在 SQL 中),但我可以评估客户端。我认为这与我将二进制属性测试为“null”有关。
我需要确定我的作业何时完成,这意味着该作业中的所有文件在其二进制 FileContents 属性中至少有一些数据。该过程是查找给定 JobId 的所有文件,如果其中任何文件的 FileContents = null,则作业尚未完成。如果所有文件都有一些文件内容,则作业完成。
第一次尝试:
bool isJobComplete = !context.Files.Any
(f => f.JobId == this.JobId && f.FileContents == null);
不行!我收到 SQL 超时异常。我还尝试了同一件事的其他变体并收到相同的结果,例如
var filesInJob = context.Files.Where(f => f.JobId == this.JobId);
bool isJobComplete = !filesInJob.Any(jf => jf.FileContents == null);
解决方案是检索所有文件的列表,然后检查 FileContents 客户端。这远非理想,因为有时我的作业中会有许多大文件。
List<File> filesInJob = context.Files.Where(f => f.JobId == this.JobId).ToList();
bool isJobComplete = !filesInJob.Any(jf => jf.FileContents == null);
在非工作表达式上生成的 SQL:
SELECT
<CASE
WHEN EXISTS<
SELECT NULL AS [EMPTY]
FROM [dbo].[File] as [t0]
WHERE <[t0].[JobId] = @p)> AND <[t0].[FileContents] IS NULL>
> THEN 1
ELSE 0
END> AS [value1]
有人有这方面的经验,或者解释为什么 LINQ 不能与 SQL 对话 null 吗?
编辑:
运行 SELECT TOP 1 * FROM [File] WHERE [FileContents] IS NULL 也会超时。这是 SQL 无法搜索二进制类型字段的问题吗?如果是这样,是否有一个 SQL 命令适用于这种情况?我们如何操作 LINQ-to-SQL 来生成这样的命令?
This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL) but I can evaluate client side. I think it's related to my testing Binary property as 'null'.
I need to determine when my Job is complete, which means that all Files in that Job have at least some data in their binary FileContents property. The procedure is to find all Files for a given JobId, and if any of those Files have FileContents = null then the Job is not yet complete. If all of the Files have some FileContents then the Job is complete.
The first attempt:
bool isJobComplete = !context.Files.Any
(f => f.JobId == this.JobId && f.FileContents == null);
does not work! I receive a SQL Timeout exception. I also tried other variations on the same thing and receive the same result, e.g.
var filesInJob = context.Files.Where(f => f.JobId == this.JobId);
bool isJobComplete = !filesInJob.Any(jf => jf.FileContents == null);
The resolution is to retrieve a List of all the Files, and then check the FileContents client-side. This is far from ideal, as sometimes I will have many large files in a Job.
List<File> filesInJob = context.Files.Where(f => f.JobId == this.JobId).ToList();
bool isJobComplete = !filesInJob.Any(jf => jf.FileContents == null);
The SQL generated on the non-working expression:
SELECT
<CASE
WHEN EXISTS<
SELECT NULL AS [EMPTY]
FROM [dbo].[File] as [t0]
WHERE <[t0].[JobId] = @p)> AND <[t0].[FileContents] IS NULL>
> THEN 1
ELSE 0
END> AS [value1]
Does anyone have any experience of this, or an explanation of why LINQ can't talk null to SQL?
EDIT:
Running SELECT TOP 1 * FROM [File] WHERE [FileContents] IS NULL also gives a timeout. Is this an issue where SQL cannot search on Binary type fields? If so, is there a SQL command that will work with this scenario and how can we manipulate LINQ-to-SQL to generate such a command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我闻起来就像有人有一把锁。当查询运行时,在服务器上运行 sp_who2。是否有什么东西阻止了查询?
Smells to me like someone might have a lock. While the query is running, run an sp_who2 on your server. Is there something blocking the query?