使用多个选项限制选择包括使用实体框架

时间:2018-01-26 13:36:11

标签: vb.net frameworks entity

我有以下代码从我的数据库成功加载一组完整的实体。我很难将此选择限制为记录,其中提交表中的字段设置为特定值。请注意,提交是一个包含在批次集合中的集合。

                Dim pl As List(Of BATCH) = db.BATCHes _
                .Include("HEADER") _
                .Include("SUBMISSIONS") _
                .Include("SUBMISSIONS.SUB_HEADER") _
                .Include("SUBMISSIONS.MA_POL") _
                .Include("SUBMISSIONS.MA_POL.MA_CUR_EMP") _
                .Include("SUBMISSIONS.MA_POL.MA_CON") _
                .Include("SUBMISSIONS.MA_POL.MA_CAD") _
                .Include("SUBMISSIONS.MA_POL.MA_TEL") _
                .Include("SUBMISSIONS.MA_POL.MA_EMA") _
                .Include("SUBMISSIONS.MA_POL.MA_BNK") _
                .Include("SUBMISSIONS.MA_POL.MA_BNK.MA_BWA_RES") _
                .Include("SUBMISSIONS.MA_POL.MA_DOC") _
                .Include("SUBMISSIONS.VEH") _
                .Include("SUBMISSIONS.VEH.VEH_GAR_ADD") _
                .Include("SUBMISSIONS.MAIN_DRI") _
                .Include("SUBMISSIONS.MAIN_DRI.MAIN_DRI_CAD") _
                .Include("SUBMISSIONS.MAIN_DRI.MAIN_DRI_TEL") _
                .Include("SUBMISSIONS.MAIN_DRI.MAIN_DRI_EMA") _
                .Include("SUBMISSIONS.MAIN_DRI.MAIN_DRI_DOC") _
                .Include("SUBMISSIONS.MAIN_DRI.MAIN_DRI_CON") _
                .Include("SUBMISSIONS.ADD_DRI") _
                .Include("SUBMISSIONS.ADD_DRI.ADD_DRI_CAD") _
                .Include("SUBMISSIONS.ADD_DRI.ADD_DRI_TEL") _
                .Include("SUBMISSIONS.ADD_DRI.ADD_DRI_EMA") _
                .Include("SUBMISSIONS.ADD_DRI.ADD_DRI_DOC") _
                .Include("SUBMISSIONS.ADD_DRI.ADD_DRI_CON") _
                .Include("SUBMISSIONS.BRO") _
                .Include("SUBMISSIONS.DEV") _
                .Include("SUBMISSIONS.DEV.DEV_IP") _
                .Include("SUBMISSIONS.DEV.DEV_PIP") _
                .ToList

我尝试过添加一个where子句,但由于提交集合不是顶层,因此无法按预期工作。

 .Where(Function(y) y.SUBMISSIONS.Select(Function(y) y.XMLBatchNumber = BatchNumber).ToList

这会导致语法错误。

如果我将相似的代码添加到.Include语句以进行提交,我没有语法错误,但代码在执行时会抛出错误。

    Dim pl As List(Of BATCH) = db.BATCHes _
                .Include("HEADER") _
                .Include(Function(x) x.SUBMISSIONS.Select(Function(y) y.XMLBatchNumber = BatchNumber)) _
                .Include("SUBMISSIONS.SUB_HEADER") _' Etc... 

错误是:' {" Include路径表达式必须引用在该类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。" &安培; vbCrLf& "参数名称:路径"}'

有人可以告诉我哪里出错了。

2 个答案:

答案 0 :(得分:0)

要使用Include,您需要将您要查找的类属性公开为实体的导航属性,即Public Property Submissions as ICollection(Of Submission)

在您的情况下,您有一个Batch实体,它应该将Submission实体集合公开为导航属性。这样您就可以使用Include.

加载它

如果您不想拥有此导航属性,可以使用Joins获取您要查找的数据,而无需使用Include

答案 1 :(得分:0)

感谢您的建议,他们帮助我找到了解决方案。

我能够通过分割我的选择来解决问题,如下所示:

 db.BATCHes.Load
 db.SUBMISSIONs.Include("SUB_HEADER") _
                        .Include("MA_POL") _
                        .Include("MA_POL.MA_CUR_EMP") _
                        .Include("MA_POL.MA_CON") _
                        .Include("MA_POL.MA_CAD") _
                        .Include("MA_POL.MA_TEL") _
                        .Include("MA_POL.MA_EMA") _
                        .Include("MA_POL.MA_BNK") _
                        .Include("MA_POL.MA_BNK.MA_BWA_RES") _
                        .Include("MA_POL.MA_DOC") _
                        .Include("VEH") _
                        .Include("VEH.VEH_GAR_ADD") _
                        .Include("MAIN_DRI") _
                        .Include("MAIN_DRI.MAIN_DRI_CAD") _
                        .Include("MAIN_DRI.MAIN_DRI_TEL") _
                        .Include("MAIN_DRI.MAIN_DRI_EMA") _
                        .Include("MAIN_DRI.MAIN_DRI_DOC") _
                        .Include("MAIN_DRI.MAIN_DRI_CON") _
                        .Include("ADD_DRI") _
                        .Include("ADD_DRI.ADD_DRI_CAD") _
                        .Include("ADD_DRI.ADD_DRI_TEL") _
                        .Include("ADD_DRI.ADD_DRI_EMA") _
                        .Include("ADD_DRI.ADD_DRI_DOC") _
                        .Include("ADD_DRI.ADD_DRI_CON") _
                        .Include("BRO") _
                        .Include("DEV") _
                        .Include("DEV.DEV_IP") _
                        .Include("DEV.DEV_PIP") _
                        .Where(Function(s) s.XMLBatchNumber = BatchNumber).Load