基于另一个LINQ到实体查询的结果更新一个实体

时间:2011-04-09 14:41:54

标签: asp.net vb.net entity-framework entity-framework-4 linq-to-entities

我有以下LINQ到实体查询:

' Get all the residency assignments that match the term/year.
        Dim assignments = From p In dbContext.Residents _
                          Where p.semester = term _
                          Where p.year = year _
                          Select p

这将为我提供当前年度/学期的所有常驻作业。然后我有这个LINQ-to-Entities查询:

 Dim reset_occupancy = From p In dbContext.Rooms _
                              Select p

这将给我所有的房间。我想迭代分配,并根据分配的房间更新reset_occupancy中的占用率。我不是100%肯定如何实现这一目标。这是我想要完成的伪代码:

For each row in assignments
   reset_occupancy.Where(reset_occupancy.room=assignment.occupancy).current_occupancy =+1
Next 

1 个答案:

答案 0 :(得分:0)

如果有人有更好的答案,我仍然会喜欢它......但对于其他尝试类似过程的人来说,我是如何解决的:

        ' Now, retabulate current occupancy.
        For Each row In assignments
            Dim assignment As Integer = row.room
            Dim update_occupancy = (From p In dbContext.Rooms _
                                   Where p.id = assignment _
                                   Select p).FirstOrDefault

            update_occupancy.current_occupancy = update_occupancy.current_occupancy + 1
        Next
        dbContext.SaveChanges()