{
  "id": "dotnet/ef-core-translation-failure",
  "signature": "System.InvalidOperationException: The LINQ expression 'x => x.SomeMethod()' 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 表达式 'x => x.SomeMethod()' 无法转换。请重写查询为可转换的形式，或通过插入对 'AsEnumerable'、'AsAsyncEnumerable'、'ToList' 或 'ToListAsync' 的调用来显式切换到客户端评估。",
  "regex": "could not be translated.*rewrite the query",
  "domain": "dotnet",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "EF Core cannot translate a LINQ method call (e.g., custom methods, ToString on non-mapped types) into SQL, because it lacks a corresponding SQL function or mapping.",
  "root_cause_type": "generic",
  "root_cause_zh": "EF Core 无法将 LINQ 方法调用（例如自定义方法、未映射类型上的 ToString）转换为 SQL，因为它没有对应的 SQL 函数或映射。",
  "versions": [
    {
      "version": "net6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "net7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "net8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "net9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding .AsEnumerable() before the problematic method call without understanding which part causes the issue",
      "why_fails": "AsEnumerable() forces client evaluation for the entire query, which may work but can cause performance issues and does not fix the root cause if the method is used multiple times.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using .Select() with a lambda that calls ToString() on a non-string property expecting EF Core to translate it",
      "why_fails": "ToString() is not translatable for many types (e.g., decimal, DateTime) in older EF Core versions; it requires a specific SQL function like CAST or FORMAT.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Changing the method to be static and assuming EF Core will recognize it",
      "why_fails": "EF Core does not automatically translate custom static methods; they must be registered as queryable functions or converted to translatable expressions.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Rewrite the query to use translatable constructs. For example, replace '.Where(x => x.CustomMethod() == value)' with '.Where(x => x.Property == value)' if CustomMethod just accesses a property. If using ToString, use 'SqlFunctions.StringConvert' for numeric types or 'EF.Functions.DateDiff' for dates.",
      "success_rate": 0.9,
      "how": "Rewrite the query to use translatable constructs. For example, replace '.Where(x => x.CustomMethod() == value)' with '.Where(x => x.Property == value)' if CustomMethod just accesses a property. If using ToString, use 'SqlFunctions.StringConvert' for numeric types or 'EF.Functions.DateDiff' for dates.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use client evaluation explicitly by calling .AsEnumerable() or .ToList() before the non-translatable part. Example: 'var results = context.Entities.Where(e => e.IsActive).AsEnumerable().Where(e => e.CustomMethod() == true).ToList();'",
      "success_rate": 0.85,
      "how": "Use client evaluation explicitly by calling .AsEnumerable() or .ToList() before the non-translatable part. Example: 'var results = context.Entities.Where(e => e.IsActive).AsEnumerable().Where(e => e.CustomMethod() == true).ToList();'",
      "condition": "",
      "sources": []
    },
    {
      "action": "Register a custom queryable function using 'HasDbFunction' in OnModelCreating. Example: 'modelBuilder.HasDbFunction(() => MyDbContext.ConvertToUpper(default))' and map it to a SQL function like 'UPPER'.",
      "success_rate": 0.8,
      "how": "Register a custom queryable function using 'HasDbFunction' in OnModelCreating. Example: 'modelBuilder.HasDbFunction(() => MyDbContext.ConvertToUpper(default))' and map it to a SQL function like 'UPPER'.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Rewrite the query to use translatable constructs. For example, replace '.Where(x => x.CustomMethod() == value)' with '.Where(x => x.Property == value)' if CustomMethod just accesses a property. If using ToString, use 'SqlFunctions.StringConvert' for numeric types or 'EF.Functions.DateDiff' for dates.",
    "Use client evaluation explicitly by calling .AsEnumerable() or .ToList() before the non-translatable part. Example: 'var results = context.Entities.Where(e => e.IsActive).AsEnumerable().Where(e => e.CustomMethod() == true).ToList();'",
    "Register a custom queryable function using 'HasDbFunction' in OnModelCreating. Example: 'modelBuilder.HasDbFunction(() => MyDbContext.ConvertToUpper(default))' and map it to a SQL function like 'UPPER'."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/ef/core/querying/client-eval",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-05-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}