致电'阅读'当数据阅读器关闭时,实体框架中的操作不是有效的

时间:2016-10-28 05:17:37

标签: c# .net entity-framework linq dbcontext

我在Windows应用程序C#中使用Entity Framework,尝试使用DbContext从两个实体检索数据,并希望进行简单连接,但我的代码细分(在var modellst行)。我的示例代码如下

        using (var ctx = new DbEntities())
        {

            var lst = ctx.AUMaterials.Where(o => o.ServiceRequestTypeId == serviceReqId && o.SSStock.Quantity > 0).ToList();
            var modellst = ctx.AUModelMaterials.Where(o => o.ModelId == modelId).ToList();

            // here i want to make join on these two list
        }

首先列出AUMaterials实体中的数千条记录。我认为加载需要很长时间。在AUModelMaterials实体中也是如此,这里也有数千条记录。但是同样的代码在早期阶段工作正常。

1 个答案:

答案 0 :(得分:1)

 var results = (from t1 in context.AUMaterials
                      join t2 in context.AUModelMaterials
                      on  t1.Col1 equals t2.Col1
                      where t1.ServiceRequestTypeId == serviceReqId && t1.SSStock.Quantity > 0 && t2.ModelId == modelId
                      select new { t1, t2}).ToList();  
多列

Joining

 var results = (from t1 in context.AUMaterials
                      join t2 in context.AUModelMaterials
                      on new {t1.Col1, t1.Col2, t1.Col3 } equals
                          new { t2.Col1, t2.Col2, t2.Col3 }
                      where t1.ServiceRequestTypeId == serviceReqId && t1.SSStock.Quantity > 0 && t2.ModelId == modelId
                      select new { t1, t2}).ToList();