Skip to content

Query: enable synthesizing rows from list/array of constant values to allow more queries to be server-evaluated #6660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
felipepessoto opened this issue Oct 2, 2016 · 7 comments

Comments

@felipepessoto
Copy link
Contributor

felipepessoto commented Oct 2, 2016

Steps to reproduce

string[] termsSplit = terms.Split(' ');
IQueryable<Post> query2 = 
    from post in db.Posts
    where termsSplit.Any(x => post.Content.Contains(x))
    select post;

The issue

query2.Count() causes a NullReferenceException. But it shouldn't be evalute that query at client side:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , String )
       at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
       at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
       at lambda_method(Closure , QueryContext )
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_1`1.<CompileQuery>b__1(QueryContext qc)
       at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
       at FujiyBlog.Web.Controllers.SearchController.Index(Nullable`1 page, String terms) in D:\Meus Arquivos\Desenvolvimento\Projetos\FujiyBlog\src\FujiyBlog.Web\Controllers\SearchController.cs:line 42
       at lambda_method(Closure , Object , Object[] )
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
  InnerException: 
@rowanmiller
Copy link
Contributor

@fujiy what version are you using? (1.0 or nightly 1.1 build)

@felipepessoto
Copy link
Contributor Author

felipepessoto commented Oct 3, 2016

1.0. Should I try with daily build?

@maumar
Copy link
Contributor

maumar commented Oct 4, 2016

Any + Contains is not the problem here. Problem is the fact that termsSplit is a client side variable. In order to make this work on the server we would have to construct a row from a list of constants, which we currently don't do. Something like what EF6 does:

SELECT N'Foo' AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT N'Bar' AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]

If we apply Any + Contains on a navigation property, e.g.:

from b in ctx.Blogs
where b.Posts.Any(p => p.Content.Contains(p.Foo))
select b;

we get a reasonable sql:

SELECT [b].[Id]
        FROM [Blogs] AS [b]
        WHERE EXISTS (
            SELECT 1
            FROM [Posts] AS [p]
            WHERE ((CHARINDEX([p].[Foo], [p].[Content]) > 0) OR ([p].[Foo] = N'')) AND ([b].[Id] = [p].[BlogId]))

@maumar maumar changed the title Any + Contains is client evaluated Query :: enable synthesizing rows from list/array of constant values to allow more queries to be server-evaluated Oct 4, 2016
@maumar maumar removed this from the 1.1.0 milestone Oct 4, 2016
@maumar
Copy link
Contributor

maumar commented Oct 4, 2016

sending for re-triage

@gojanpaolo
Copy link

gojanpaolo commented Apr 1, 2020

I believe this is the issue we're having right now.

var words = new[] { "foo", "bar" };
ctx.Post.Where(p => words.Any(p.Title.Contains));

We used SearchExtensions as a workaround. But hopefully translating Any + Contains gets built-in ef core as well.

ctx.Post.Search(p => p.Title).Containing(words);

@nbelley
Copy link

nbelley commented Oct 27, 2021

I used raw sql queries instead... kind of sucks to be forced to do that!

@AndriySvyryd AndriySvyryd changed the title Query :: enable synthesizing rows from list/array of constant values to allow more queries to be server-evaluated Query: enable synthesizing rows from list/array of constant values to allow more queries to be server-evaluated Jan 14, 2022
@maumar maumar assigned roji and unassigned maumar Apr 27, 2023
@roji
Copy link
Member

roji commented Apr 27, 2023

Duplicate of #30426

@roji roji marked this as a duplicate of #30426 Apr 27, 2023
@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants