【问题标题】:Linq Multiple join to 'Only primitive types or enumeration types are supported' ErrorLinq 多连接到“仅支持原始类型或枚举类型”错误
【发布时间】:2019-09-11 13:14:51
【问题描述】:

我正在使用实体框架测试我的应用程序,并遇到了这个错误:

“无法创建类型的常量值此上下文仅支持原始类型或枚举类型。”

但是,我想不出解决问题的方法?

var products = GeneralContext.Inventory
.Join(GeneralContext.Products,
    invtry => invtry.ProductId,
    prdct => prdct.ProductId,
   (invtry, prdct) => new { Inventory = invtry, Product = prdct })
.Where(q =>
    q.Inventory.PlaceId == (int)model.PlaceId &&
    q.Inventory.InventoryStatu == (int)Enums.Reason.Stok &&
    q.Product.IsWithRecipe == false &&
    boxes.Any(c=>c.Inventory.ProductCode == q.Inventory.ParentProductCode))
.ToList();

【问题讨论】:

  • 从boxes 获取代码列表并改用它。
  • boxes 是另一个新的 { Inventory = invtry, Product = prdct }) 的产品列表

标签: c# linq


【解决方案1】:

您需要预先创建要过滤的代码集合并使用它而不是复杂类型的集合。您可能还需要对地点 ID 执行相同操作。

var codes = boxes.Select(c => c.Inventory.ProductCode).ToList();
int placeId = (int)model.PlaceId;

var products = GeneralContext.Inventory
.Join(GeneralContext.Products,
    invtry => invtry.ProductId,
    prdct => prdct.ProductId,
   (invtry, prdct) => new { Inventory = invtry, Product = prdct })
.Where(q =>
    q.Inventory.PlaceId == placeId &&
    q.Inventory.InventoryStatu == (int)Enums.Reason.Stok &&
    q.Product.IsWithRecipe == false &&
    codes.Contains(q.Inventory.ParentProductCode))
.ToList();

基本上,EF 限制了您可以使用的内容,因为它试图将代码转换为 SQL,并且无法创建 SQL 代码来处理复杂类型。

【讨论】:

    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多