Home > Enterprise >  Does explicit client evaluation of where clause require AsEnumerable?
Does explicit client evaluation of where clause require AsEnumerable?

Time:07-11

StandardizeUrl is function written in c#. Suppose it is used in the select query then the function is applied in memory after the data is returned by sql - example:

var blogs = context.Blogs
    .OrderByDescending(blog => blog.Rating)
    .Select(
        blog => new { Id = blog.BlogId, Url = StandardizeUrl(blog.Url) })
    .ToList();

This question is about using a custom c# function in WHERE clause. The below return runtime exception:

var blogs = context.Blogs
    .Where(blog => StandardizeUrl(blog.Url).Contains("dotnet"))
    .ToList();

Solution is given here: Link- https://docs.microsoft.com/en-us/ef/core/querying/client-eval#explicit-client-evaluation

var blogs = context.Blogs
    .AsEnumerable()
    .Where(blog => StandardizeUrl(blog.Url).Contains("dotnet"))
    .ToList();

Why does the link says we can either use AsEnumerable or ToList. For example if I don't use AsEnumerable then it gives an error.

CodePudding user response:

The point is that you get "client evaluation" with methods like AsEnumerable() or ToList() before using the Where() call. That way the data is (fully) transferred to the client (your C# application) and then filtered on the client side, where you have access to your StandardizeUrl() method. The documentation does not talk about the ToList() after the Where() call.

  • Related