Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 比较两个查询并同步结果_Asp.net_Vb.net_Entity Framework_Linq To Entities - Fatal编程技术网

Asp.net 比较两个查询并同步结果

Asp.net 比较两个查询并同步结果,asp.net,vb.net,entity-framework,linq-to-entities,Asp.net,Vb.net,Entity Framework,Linq To Entities,我有一张桌子(房间),里面有很多行。每行代表一个房间,每个房间需要存在两次(秋季一次,春季一次)。有时候,当人们增加一个房间时,他们只增加一个学期。我正在研究一个在学期之间同步房间的过程 首先,我提取了两个查询,一个是在学期专栏中获取所有包含秋天的房间,另一个是在学期专栏中获取所有包含春天的房间,如下所示: Dim getFallRooms = (From p In dbContext.Rooms _ Where p.sem

我有一张桌子(房间),里面有很多行。每行代表一个房间,每个房间需要存在两次(秋季一次,春季一次)。有时候,当人们增加一个房间时,他们只增加一个学期。我正在研究一个在学期之间同步房间的过程

首先,我提取了两个查询,一个是在学期专栏中获取所有包含秋天的房间,另一个是在学期专栏中获取所有包含春天的房间,如下所示:

        Dim getFallRooms = (From p In dbContext.Rooms _
                            Where p.semester = "Fall" _
                            Select p)
        Dim getSpringRooms = (From p In dbContext.Rooms _
                              Where p.semester = "Spring" _
                              Select p)
每个结果将包含多行,其中包含以下列:id、building、room、Occumber和Serment

我想做的是这样的事情(伪):


关于如何将其转化为实际可行的代码,您有什么建议吗?

您可以做一些与正在做的非常类似的事情

我不知道如何在VB.NET中准确地执行此操作,但以下是我将如何在C#中使用LINQ扩展方法执行此操作:

var fallRooms = dbContext.Rooms.Where(room => room.semester.Equals("Fall")).Select(r => r.Name);
var nonSpringRooms = dbContext.Rooms.Where(room => room.semester.Equals("Spring"))
                                    .Select(r => r.Name)
                                    .AsQueryable()
                                    .Except(fallRooms);
之后,您可以对非SpringRooms上的每个循环执行
,以执行您想要执行的任何操作

另外,如果我错了,应该有人纠正我,但我相信上面的代码只会进入数据库一次。因此,你也会从中受益

编辑:我意识到,由于它们位于同一个表中,所以记录将具有相同的主键。我随后更改了上面的查询,假设您在两个记录中有一个相同的名称字段

For Each row in getFallRooms
    If (From p in getSpringRooms 
        Where p.building = row.building 
        AndAlso p.room = row.room).Count = 0 Then
       ' Code to add new row with fall as the semester.
    End If
Next
For Each row in getFallRooms
    If (From p in getSpringRooms 
        Where p.building = row.building 
        AndAlso p.room = row.room).Count = 0 Then
       ' Code to add new row with fall as the semester.
    End If
Next