调用& # 39;读# 39;当数据读取器关闭时,在实体框架中不是一个有效的操作

本文关键字:一个 操作 有效 框架 数据 读取 调用 实体 | 更新日期: 2023-09-27 18:15:24

我在windows应用程序c#中使用实体框架,试图使用DbContext从两个实体检索数据,并希望进行简单的连接,但我的代码崩溃(在var modelst行)。我的示例代码如下

        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
        }

在这里的第一个列表数以千计的记录在aummaterials实体。而且我觉得装子弹要花很多时间。在AUModelMaterials实体中也是这样,这里也有数千条记录。但是同样的代码在早期阶段工作得很好。

调用& # 39;读# 39;当数据读取器关闭时,在实体框架中不是一个有效的操作

 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();  

多列连接

 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();