System.InvalidOperationException: 'The LINQ expression => EntityShaperExpression:

Tam Luong 20 Reputation points
2023-07-14T07:32:31.3533333+00:00
var searchs = new List<string> { "a1", "a2", "a3" };
var result = db.Products.Where(p => searchs.Any(s => p.Content.Contains(s))).ToList();

my query like that run ok in asp.net mvc 5 (vs 2019)

but when i use vs 2022 with asp.net mvc core .net 6, this query error, i don't know why

sorry my English not good.

(Content is a string)

System.InvalidOperationException: 'The LINQ expression 's => EntityShaperExpression: 
    Website.Models.Product
    ValueBufferExpression: 
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False
.Content.Contains(s)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'


Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-14T08:23:48.6433333+00:00

    Hi @Tam Luong

    System.InvalidOperationException: 'The LINQ expression 's => EntityShaperExpression: Website.Models.Product ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False .Content.Contains(s)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

    As the error message said, you need to rewrite the query statement. Try to add AsEnumerable()or ToList() before the where clause.

    After modifying the code as below:

    var searchs = new List<string> { "a1", "a2", "a3" }; 
    var result = db.Products.AsEnumerable().Where(p => searchs.Any(s => p.Content.Contains(s))).ToList();
    

    Or

    var searchs = new List<string> { "a1", "a2", "a3" }; 
    var result = db.Products.ToList().Where(p => searchs.Any(s => p.Content.Contains(s))).ToList();
    

    The result on my side as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.