c# - 将 Include() 重写为 linq 连接

标签 c# linq entity-framework compiled-query

由于(目前)不可能在编译查询中使用 Include(),我正在尝试将它们重写为连接。但它似乎并没有像我希望的那样成功。

假设我有这个关系:

Order              OrderState
Id                 Id   
OrderStateId       Description

现在我用来获取描述如下:

var q = (from o in context.Orders
         where o.Id = orderId
         select o).Include("OrderState");

我试过将其重写为:

var q = (from o in context.Orders
         join st in context.OrderStates on o.OrderStateId equals st.Id
         where o.Id = orderId
         select o);

但 OrderState 在我的结果集中仍然为空。我将如何解决这个问题(以编译查询可接受的方式)?

最佳答案

如果您不能使用 .Include,您实际上需要在结果中的某处包含 o.OrderState,那么这个怎么样?

var q = (from o in context.Orders
         where o.Id = orderId
         select new { Order = o, o.OrderState });

o.Order.OrderState 应该连接到 o.OrderState 而无需为 q 中的每个结果提供额外帮助。

关于c# - 将 Include() 重写为 linq 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11011628/

相关文章:

c# - VS2017调试Razor类库时"#"sign cannot be in csproj/sln pathname

c# - 在 DataTriggers 中重用 ControlTemplate

c# - 参数化 Where IN 子句不适用于 CosmosClient QueryDefinition 对象

linq - 使用 LINQ 连接两个表并将多个记录作为一行返回

c# - Entity Framework Core 表拆分方法

entity-framework - 多对多关系无法挽救

c# - Entity Framework Code First 从两个表和一对多关系创建类

c# - 在 C# 中监听另一个窗口调整大小事件

c# - Linq to Entities 和串联属性

c# - 如何从 LINQ 查询中获取生成的 Lucene 查询