Local sequence cannot be used in LINQ to SQL

The local sequence cannot be used in LINQ to SQL implementations of query statements other than the Contains statement.

I get this error from the following linq request:

List<Something> results = new List<Something>(items .Where(w => selecteditems.Count == 0 || w.ops.Intersect(selecteditems).Count() > 0) .ToList() .OrderBy(a => a.FirstNumber) .OrderBy(b => b.SecondNumber)); 

Would this intersection cross this error?

+4
source share
1 answer

The query provider does not know how to translate w.ops.Intersect(selecteditems) into an SQL query.

If selecteditems was another query from the same query provider, then he could translate them, or if the whole operation was performed in Linq-to-Objects, and not Linq-to-SQL, then be smart.

According to the error message, the only operation that he knows how to perform on such an object is Contains . Instead, you can reuse your request:

 .Where(w => selecteditems.Count == 0 || w.ops.Any(op => selecteditems.Contains(op))) 

This [should] work.

+5
source

Source: https://habr.com/ru/post/1501554/


All Articles