{
  "id": "dotnet/ef-core-query-cannot-be-translated-into-a-sql-expression",
  "signature": "System.InvalidOperationException: The LINQ expression 'DbSet<X>.Where(x => x.Property.Contains(\"value\") || x.OtherProperty.Contains(\"other\"))' 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'.",
  "signature_zh": "System.InvalidOperationException: LINQ 表达式 'DbSet<X>.Where(x => x.Property.Contains(\"value\") || x.OtherProperty.Contains(\"other\"))' 无法翻译。请以可翻译的形式重写查询，或通过插入对 'AsEnumerable'、'AsAsyncEnumerable'、'ToList' 或 'ToListAsync' 的调用来显式切换到客户端评估。",
  "regex": "The LINQ expression '.*' 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'\\.",
  "domain": "dotnet",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Entity Framework Core cannot translate a LINQ expression into SQL when it uses unsupported constructs like certain string methods, custom functions, or complex logic that the database provider does not support.",
  "root_cause_type": "generic",
  "root_cause_zh": "当 LINQ 表达式使用数据库提供程序不支持的某些字符串方法、自定义函数或复杂逻辑等构造时，Entity Framework Core 无法将其翻译为 SQL。",
  "versions": [
    {
      "version": "Entity Framework Core 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Entity Framework Core 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Microsoft.EntityFrameworkCore.SqlServer 8.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Calling 'ToList()' before the filter forces client evaluation but loads all data into memory, causing performance issues and potential out-of-memory errors.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Adding 'AsEnumerable()' at the start of the query chain also forces full client evaluation, defeating the purpose of database filtering.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Ignoring the error and assuming the query works on a different database provider (e.g., SQLite vs SQL Server) leads to runtime failures when deploying.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Rewrite the LINQ expression using only database-supported constructs. For example, replace 'x.Property.Contains(\"value\") || x.OtherProperty.Contains(\"other\")' with 'EF.Functions.Like(x.Property, \"%value%\") || EF.Functions.Like(x.OtherProperty, \"%other%\")' which translates to SQL LIKE.",
      "success_rate": 0.85,
      "how": "Rewrite the LINQ expression using only database-supported constructs. For example, replace 'x.Property.Contains(\"value\") || x.OtherProperty.Contains(\"other\")' with 'EF.Functions.Like(x.Property, \"%value%\") || EF.Functions.Like(x.OtherProperty, \"%other%\")' which translates to SQL LIKE.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the query cannot be rewritten, use client evaluation explicitly but filter as much as possible on the server side first. For example: 'var filtered = await context.Orders.Where(o => o.Status == \"Active\").ToListAsync(); var result = filtered.Where(o => o.Description.Contains(\"keyword\")).ToList();'",
      "success_rate": 0.8,
      "how": "If the query cannot be rewritten, use client evaluation explicitly but filter as much as possible on the server side first. For example: 'var filtered = await context.Orders.Where(o => o.Status == \"Active\").ToListAsync(); var result = filtered.Where(o => o.Description.Contains(\"keyword\")).ToList();'",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "仅使用数据库支持的构造重写 LINQ 表达式。例如，将 'x.Property.Contains(\"value\") || x.OtherProperty.Contains(\"other\")' 替换为 'EF.Functions.Like(x.Property, \"%value%\") || EF.Functions.Like(x.OtherProperty, \"%other%\")'，这会翻译为 SQL LIKE。",
    "如果无法重写查询，则显式使用客户端评估，但首先在服务器端尽可能过滤。例如：'var filtered = await context.Orders.Where(o => o.Status == \"Active\").ToListAsync(); var result = filtered.Where(o => o.Description.Contains(\"keyword\")).ToList();'"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/ef/core/querying/client-evaluation#client-evaluation",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-07-18",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}