EntityFramework Any in Where

Selçuk Yıldırım 1 Reputation point
2021-02-18T11:06:17.587+00:00

Hello, when i try to use Any in EF Core where, i get an error. can you help?

query:

        var identifierslist = new List<DeviceIdentifierModel>(){..identifiers here...};

        var devices = await _context.Device.AsNoTracking()
            .Where(x =>
                identifierslist.Any(z =>
                    (z.UtilityAppIdentifierInt1 == null || z.UtilityAppIdentifierInt1 == x.UtilityAppIdentifierInt1)
                    && (z.UtilityAppIdentifierInt2 == null ||
                        z.UtilityAppIdentifierInt2 == x.UtilityAppIdentifierInt2)
                    && (z.UtilityAppIdentifierInt3 == null ||
                        z.UtilityAppIdentifierInt3 == x.UtilityAppIdentifierInt3)
                    && (z.UtilityAppIdentifierString1 == null ||
                        z.UtilityAppIdentifierString1 == x.UtilityAppIdentifierString1)
                    && (z.UtilityAppIdentifierString2 == null ||
                        z.UtilityAppIdentifierString2 == x.UtilityAppIdentifierString2)
                    && (z.UtilityAppIdentifierGuid1 == null ||
                        z.UtilityAppIdentifierGuid1 == x.UtilityAppIdentifierGuid1)
                )
            ).ToListAsync(); 

DeviceIdentifierModel:

public class DeviceIdentifierModel
{
    public int? UtilityAppIdentifierInt1 { get; set; }
    public int? UtilityAppIdentifierInt2 { get; set; }
    public int? UtilityAppIdentifierInt3 { get; set; }
    public string UtilityAppIdentifierString1 { get; set; }
    public string UtilityAppIdentifierString2 { get; set; }
    public Guid? UtilityAppIdentifierGuid1 { get; set; }
}

error:

System.InvalidOperationException: The LINQ expression 'DbSet<Device>()
    .Where(d => __identifiers_0
        .Any(z => z.UtilityAppIdentifierInt1 == null || z.UtilityAppIdentifierInt1 == d.UtilityAppIdentifierInt1 && z.UtilityAppIdentifierInt2 == null || z.UtilityAppIdentifierInt2 == d.UtilityAppIdentifierInt2 && z.UtilityAppIdentifierInt3 == null || z.UtilityAppIdentifierInt3 == d.UtilityAppIdentifierInt3 && z.UtilityAppIdentifierString1 == null || z.UtilityAppIdentifierString1 == d.UtilityAppIdentifierString1 && z.UtilityAppIdentifierString2 == null || z.UtilityAppIdentifierString2 == d.UtilityAppIdentifierString2 && z.UtilityAppIdentifierGuid1 == null || z.UtilityAppIdentifierGuid1 == d.UtilityAppIdentifierGuid1))' 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.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at SamaMap.Data.Processing.Storage.Repositories.RecordRepository.GetLatestRecordsFromIdentifiers(DeviceIdentifierModel[] identifiers) in /Users/Selcuk/Documents/35inch/sama/core_service/src/backend/SamaMap.Data.Processing/Storage/Repositories/RecordRepository.cs:line 37
   at SamaMap.Data.Processing.Handlers.WebSocket.RealTimeTrackHandler.OnLoginPayload(String connectionId, RealTimeTrackPayload payload) in /Users/Selcuk/Documents/35inch/sama/core_service/src/backend/SamaMap.Data.Processing/Handlers/WebSocket/RealTimeTrackHandler.cs:line 101
   at SamaMap.Data.Processing.Handlers.WebSocket.RealTimeTrackHandler.ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, Byte[] buffer) in /Users/Selcuk/Documents/35inch/sama/core_service/src/backend/SamaMap.Data.Processing/Handlers/WebSocket/RealTimeTrackHandler.cs:line 65

note: i don't want use 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync' before where

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
{count} votes