EF Core 复杂的 where 子句
EF Core complex where clause
EF Core 3.1 在 运行 以下查询时抛出,抱怨它无法为其生成正确的 SQL。
var searchPatterns = new string[] { "a", "b", "c" };
var matches = from entity in _dbContext.Entity
where searchPatterns.Any(x => entity.Column1.Contains(x))
select entity;
在原始 sql 中,这可以转化为
select * from entity
where exists (select x from @SearchPatterns where entity.column1 like '%:' + x + '%'))
(其中 @SearchPatterns
是保存记录 a
、b
和 c
的 table 参数)
如何重写查询以使 EF Core 能够接受它?
编辑 我正在构建的实际查询比我上面提供的简化版本复杂得多。因此,我不考虑 FromSqlRaw()
作为我愿意使用的选项。
您可以使用原始 sql。参见:Raw SQL Queries
var blogs = context.Blogs
.FromSqlRaw("SELECT * FROM dbo.Blogs")
.ToList();
更多信息位于:Executing Raw SQL Queries
此处描述了其他选项:Breaking changes included in EF Core 3.x - 不再在客户端评估 LINQ 查询。
EF Core 3.1 在 运行 以下查询时抛出,抱怨它无法为其生成正确的 SQL。
var searchPatterns = new string[] { "a", "b", "c" };
var matches = from entity in _dbContext.Entity
where searchPatterns.Any(x => entity.Column1.Contains(x))
select entity;
在原始 sql 中,这可以转化为
select * from entity
where exists (select x from @SearchPatterns where entity.column1 like '%:' + x + '%'))
(其中 @SearchPatterns
是保存记录 a
、b
和 c
的 table 参数)
如何重写查询以使 EF Core 能够接受它?
编辑 我正在构建的实际查询比我上面提供的简化版本复杂得多。因此,我不考虑 FromSqlRaw()
作为我愿意使用的选项。
您可以使用原始 sql。参见:Raw SQL Queries
var blogs = context.Blogs
.FromSqlRaw("SELECT * FROM dbo.Blogs")
.ToList();
更多信息位于:Executing Raw SQL Queries
此处描述了其他选项:Breaking changes included in EF Core 3.x - 不再在客户端评估 LINQ 查询。