WCF 数据服务 - .Expand 或 .LoadProperty 似乎都不能满足我的需要

发布于 2024-08-29 07:40:43 字数 864 浏览 6 评论 0原文

我正在构建一个学校管理应用程序,用于跟踪学生的迟到和缺勤情况。我有三个实体可以在这方面帮助我。学生实体(名字、姓氏、ID 等); SystemAbsenceTypes 实体,具有迟到、有原因缺席、无原因缺席的 SystemAbsenceTypeID 值;以及名为 StudentAbsences 的交叉引用表(将学生 ID 与缺勤类型 ID 进行匹配,加上日期和注释字段)。

我想要做的是查询给定学生的实体,然后将给定日期范围内每种缺勤的数量相加。我毫无问题地准备了我的 currentStudent 对象,然后我这样做...

Me.Data.LoadProperty(currentStudent, "StudentAbsences") '加载交叉引用数据

lblDaysLate.Text = (From ab In currentStudent.StudentAbsencesWhere ab.SystemAbsenceTypes .SystemAbsenceTypeID = Common.enuStudentAbsenceTypes.Late).Count.ToString

...第二行失败,抱怨“对象引用未设置为对象的实例”。

我认为问题在于,虽然它确实看到(比方说)当前学生有四次缺勤(即 currentStudent.StudentAbsences.Count = 4),但它还无法“查看”每一项缺勤情况看看它的类型。事实上,四个 StudentAbsence 对象中的每一个都有一个名为 SystemAbsenceType 的属性,该属性最终具有 SystemAbsenceTypeID。

如何使用 .Expand 或 .LoadProperty 来实现此目的?我是否需要盲目地循环所有这些集合,在所有内容上触发 .LoadProperty,然后才能进行查询?

还有其他技术吗?

I am building a school management app where they track student tardiness and absences. I've got three entities to help me in this. A Students entity (first name, last name, ID, etc.); a SystemAbsenceTypes entity with SystemAbsenceTypeID values for Late, Absent-with-Reason, Absent-without-Reason; and a cross-reference table called StudentAbsences (matching the student IDs with the absence-type ID, plus a date, and a Notes field).

What I want to do is query my entities for a given student, and then add up the number of each kind of Absence, for a given date range. I prepare my currentStudent object without a problem, then I do this...

Me.Data.LoadProperty(currentStudent, "StudentAbsences") 'Loads the cross-ref data

lblDaysLate.Text = (From ab In currentStudent.StudentAbsences Where ab.SystemAbsenceTypes.SystemAbsenceTypeID = Common.enuStudentAbsenceTypes.Late).Count.ToString

...and this second line fails, complaining "Object reference not set to an instance of an object."

I presume the problem is that while it DOES see that there are (let's say) four absences for the currentStudent (ie, currentStudent.StudentAbsences.Count = 4) -- it can't yet "peer into" each one of the absences to look at its type. In fact, each of the four StudentAbsence objects has a property called SystemAbsenceType, which then finally has the SystemAbsenceTypeID.

How do I use .Expand or .LoadProperty to make this happen? Do I need to blindly loop through all these collections, firing off .LoadProperty on everything before I can do my query?

Is there some other technique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-09-05 07:40:43

当您加载学生时,尝试扩展相关属性。

var currentStudent = context.Students.Expand("StudentAbsences")
                                     .Expand("StudentAbsences/SystemAbsenceTypes")
                                     .Where(....).First();

When you load the student, try expanding the related properties.

var currentStudent = context.Students.Expand("StudentAbsences")
                                     .Expand("StudentAbsences/SystemAbsenceTypes")
                                     .Where(....).First();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文