【问题标题】:Update one Entity Based on Results of Another LINQ-to-Entities Query根据另一个 LINQ-to-Entities 查询的结果更新一个实体
【发布时间】:2011-04-09 14:41:54
【问题描述】:

我有以下 LINQ-to-Entities 查询:

' 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 

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多