简体   繁体   English

SQL:嵌套查询没有适当的键

[英]SQL: Nested query does not have appropriate key

In LINQ, I am trying to inner join custom function written for full-text search and an Iqueryable result. 在LINQ中,我试图将内部联接自定义函数编写为全文搜索和可查询的结果。

However, I get the following error when I try to to_ret.select(--something--).ToList() 但是,当我尝试to_ret.select(--something--).ToList()时出现以下错误

Nested query does not have appropriate key 嵌套查询没有适当的键

LINQ Code: LINQ代码:

  var sql_query = db.search(st);
  var to_ret = from ts in sql_query
               from t in table
               where t.Id == ts.Value select t;

  to_ret = to_ret.Include(x => x.table1)
                .Include(x=> x.table2.Select(y=> y.table2Col));

  to_ret.select(-something-).toList();

SQL Code: SQL代码:

create function [dbo].[search]
      (@keywords nvarchar(4000))
returns table
as
  return (
  select [key] from containstable(tb,(Name,Description),@keywords)
)

Code that works in place of above LINQ Code : 代替上述LINQ代码的代码:

var ids = (from t in table join ts in db.search(st) on t.Id equals ts.Value select t.Id).ToList();

to_ret = to_ret.Where(x => ids.Contains(x.Id));

However, the code that works isn't efficient enough as it eagerly loads all the ids for comparison 但是,有效的代码效率不高,因为它急切地加载所有ids以进行比较

Do not join the table using LINQ, it is not effective. 不要使用LINQ加入表,这是无效的。

You need to include all the joined functions into [dbo].[search] table valued function (like a view). 您需要将所有联接的函数包括在[dbo]。[search]表值函数中(如视图)。 Then do just call the [dbo].[search] from EF and filter it. 然后,只需从EF调用[dbo]。[search]并将其过滤即可。

I have mentioned joined Fulltext table valued function here. 我在这里提到了连接全文表值函数。

Note that fulltext and filtering together in one query could take time, because it is not easy job for query optimizer. 请注意,在一个查询中将全文和过滤一起使用可能会花费一些时间,因为对于查询优化器而言,这并非易事。 Query optimizer selects to perform first fulltext on entire table(s) and then filtering or the opposite way. 查询优化器选择对整个表执行第一个全文,然后进行过滤或采用相反的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM