'.NetCore 3.1 Linq to SQL : List within query

Earlier .NetCore versions allowed several client evaluation strategies, but starting in .NetCore 3.1 stricter rules require updates of badly written queries.

Consider this LINQ query (with EntityFrameworkCore 3.1.0):

var searchTerms = new List<string>() { "breaking", "changes" };
var topicQuery = _repository.Query<Topic>()
    .Where(topic => searchTerms
        .Any(searchTerm => topic.Title.Contains(searchTerm)));

At runtime it will throw an evaluation exception because it cannot translate the inner part of the query which requires introduction and iteration of searchTerms.

How can his query be rewritten (for .NetCore 3.1) without having to fetch all topics first from the repo, while still keeping the test for searchTerm within the inner loop?



Solution 1:[1]

Instead of iterating the searchTerms over each topic.title, you could just check if the searchTerms contains topic.Title. The query could be rewritten to achieve the requirement, something like

.Where(topic=>searchTerms.Contains(topic.Title))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharath N S