score:2
Check client and server side evaluation here: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
EF Core supports partial client evaluation in the top-level projection (essentially, the last call to Select()). If the top-level projection in the query can't be translated to the server, EF Core will fetch any required data from the server and evaluate remaining parts of the query on the client. If EF Core detects an expression, in any place other than the top-level projection, which can't be translated to the server, then it throws a runtime exception.
Your ToList
and then Any
can't be translated to sql and thus you get the error.
This would work
var results = _context.Tb1
.Join(_context.Tb2, oc => oc.OptionId, o => o.OptionId, (oc, o) => new { OptionCost = oc, Option = o })
.AsEnumerable()
.Where(x => x.Tb2.Val1 == 1
&& x.Tb2.Val2 == null
&& ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))
BUT it would first fetch everything from the server and then apply the where
clause, resulting to poor performance.
You could make it a bit better like this
var results = _context.Tb1
.Join(_context.Tb2, oc => oc.OptionId, o => o.OptionId, (oc, o) => new { OptionCost = oc, Option = o })
.Where(x => x.Tb2.Val1 == 1
&& x.Tb2.Val2 == null)
.AsEnumerable()
.Where(ocList.ToList().Any(e => e.Vdate == x.Tb2.ADate))
Source: stackoverflow.com
Related Articles
- Linq expression could not be translated Error Entity Framework Core 3
- LINQ expression error could not be translated
- The LINQ expression could not be translated - Error with Nullable Id field
- The LINQ expression could not be translated and will be evaluated locally
- The LINQ expression could not be translated
- The LINQ expression could not be translated. Eiither rewrite the query in a form that can be translated
- Why could the F# LINQ expression not be translated - at least partially?
- LINQ Expression - Using .Any in s Select Could Not Be Translated
- C# Linq Expression could not be Translated
- Generic implementation of querying - LINQ expression could not be translated
- The LINQ expression could not be translated - Entity Framework Core
- The LINQ expression could not be translated about sum of Property TimeSpan
- EF Core: The LINQ expression could not be translated
- LINQ expression could not be translated
- System.InvalidOperationException: 'The LINQ expression could not be translated. Either rewrite the query in a form that can be translated
- The LINQ expression 'Expression' could not be translated. Either rewrite the query in a form that can be translated
- InvalidOperationException: The LINQ expression could not be translated
- The LINQ expression could not be translated although ToLists is used
- LINQ to SQL - Expression checking for null could not be translated
- EF Core Linq expression could not be translated
- LINQ DateTimeOffset expression could not be translated
- The LINQ expression 'GroupByShaperExpression: could not be translated while using GROUPBY in LINQ query
- How to efficiently rewrite LINQ expression 'DbSet.Where(...Any(...))' that could not be translated in EFCore 3.1?
- EF Core: The LINQ expression could not be translated - Net Core 3.1
- Get next element from Query after element with specified Id. The LINQ expression could not be translated
- EF Core 3.1 - The LINQ expression could not be translated (left joins with group by)
- EF Core: The LINQ expression could not be translated for a nested List with Generic StartsWith() Expression
- The LINQ expression could not be translated while trying to sort
- Comparing dates within LINQ gives error - Method cannot be translated into a store expression
- C# Linq error 'DbSet() .GroupJoin( inner: DbSet(), outerKeySelector: ' could not be translated
- Nhibernate generating OUTER JOIN for a fetch
- How to project enum to id, value anon type?
- If it's bad to use inline SQL, how does using LINQ to perform queries differ in practice?
- Using LINQ for quering in a big database take so much time
- How to select all from datatable under some condition?
- Guidance with a translation in Linq to Xml
- Why won't this Linq compile?
- Return value of stored procedure is correct but column values are all NULL
- Get specific value from GroupBy Key in foreach
- Lambda Expression equivalent of "<>" in from clause of SQL with Join
- How to give the user to create dynamic data?
- Does LinqDataSource perform Server-Side paging by default?
- Using LINQ and a dropdownlist to enter a new record into an SQL Database
- nested foreach as LINQ with chained linked List
- Merge List<PartialColumn> into <List<Column> using LINQ
- How to group by in Entity Framework Core with no repetitive group?
- How to get value of element with XDocument and Linq to XML
- C# LINQ pivoting on table
- How to get the Sum of specific property using dynamic query
- Self JOIN linq with not equal