VB.Net遍历一个IQueriable(对象A)并检查属性是否存在于另一个IQueriable(对象B)中

时间:2019-10-15 16:10:33

标签: vb.net iqueryable

我有两个IQueriable wsSelectedApps和allApps 我需要循环遍历allApps中的每个应用程序,并检查其他IQueriable中是否存在属性app.AppID。

到目前为止,我的代码是...。

        Dim wsSelectedApps = (From i In de.vw_AppsForWsList
                          Where i.WSID = WSID
                          Select i.ID, i.AppID)

        Dim allApps = (From a In de.TblApps
                       Select a.AppID, a.AppName)

        Dim appList As New List(Of DeploymentModel)

        For Each app In allApps
            'Loop through allApps and build up appList

            If xxxxxxxxxx Then
                'this app exists in wsSelectedApps
                appList.Add(New DeploymentModel With {
                    .AppID = app.AppID,
                    .AppName = app.AppName,
                    .ID = wsSelectedApps.ID,
                    .Selected = True })
            Else
                'this app does not exist in wsSelectedApps
                appList.Add(New DeploymentModel With {
                    .AppID = app.AppID,
                    .AppName = app.AppName,
                    .ID = 0,
                    .Selected = False})
            End If
        Next

IF语句最初具有If wsSelectedApps.Contaings(app.AppID)然后...

当wsSelectedApps只是AppID列表时,这很好,但是现在包含两个属性。如何检查wsSelectedApps中是否存在app.AppID?

3 个答案:

答案 0 :(得分:2)

您可以使用LINQ中的左外部联接来实现。这类似于SQL,但是使用LINQ可以在找不到匹配项时指定默认值。我希望这对您有用-在您对数据库架构的了解有限的情况下,我会尽我所能。

Dim appList =
    (From a In de.tblApps
     Group Join s In de.vw_AppsForWsList On a.AppID Equals s.AppID Into Group
     From p In Group.DefaultIfEmpty(New AppsForWsList With {.AppID = a.AppID, .AppName = a.AppName, .ID = 0})
     Select New DeploymentModel With {.AppID = p.AppID, .AppName = p.AppName, .ID = p.ID, .Selected = p.ID <> 0}).ToList()

有关vb.net LINQ左外部联接的更多信息,请参见this answer

答案 1 :(得分:1)

您可以使用const screensChosenConfig = { duration: 0 }; const standardScreensConfig = { duration: 1000, easing: Easing.out(Easing.poly(4)), timing: Animated.timing, useNativeDriver: true }; transitionConfig: sceneProps => ({ transitionSpec: sceneProps.scene.route.routeName === "Account" || sceneProps.scene.route.routeName === "ChangePassword" ? screensChosenConfig : standardScreensConfig, screenInterpolator: sceneProps => { const { position, layout, scene } = sceneProps; const thisSceneIndex = scene.index; const width = layout.initWidth; const translateX = position.interpolate({ inputRange: [thisSceneIndex - 1, thisSceneIndex, thisSceneIndex + 1], outputRange: [width, 0, 0] }); const opacity = position.interpolate({ inputRange: [thisSceneIndex - 1, thisSceneIndex], outputRange: [0, 1] }); const translateWithOpacity = { opacity, transform: [{ translateX }] }; return translateWithOpacity; } }) 提供的Any()扩展名。
您的if条件如下所示

System.LINQ

答案 2 :(得分:1)

您可以使用快捷方式,直接从源列表中创建True,False列表,如下所示:

Dim TrueList = (From a As DeploymentModel In de.TblApps, b As DeploymentModel In de.vw_AppsForWsList Where b.WSID = WSID AND  a.AppID = b.AppID Select a).ToList
Dim FalseList = de.TblApps.Except(TrueList).ToList

除非您需要创建DeploymentModel对象的新列表,否则可以按如下所示修改DeploymentModel对象的属性:

TrueList.ForEach(Sub(a)
                    a.Selected = True
                    a.ID = ...
                    '...
                End Sub)

FalseList.ForEach(Sub(a)
                    a.Selected = False
                    a.ID = ...
                    '...
                End Sub)

然后,在快速测试的输出下面:

True List:

Id: 2, AppId: 1002, AppName: Application 2, Selected: True
Id: 4, AppId: 1004, AppName: Application 4, Selected: True
Id: 6, AppId: 1006, AppName: Application 6, Selected: True
Id: 8, AppId: 1008, AppName: Application 8, Selected: True
Id: 10, AppId: 1010, AppName: Application 10, Selected: True

False List:

Id: 1, AppId: 1001, AppName: Application 1, Selected: False
Id: 3, AppId: 1003, AppName: Application 3, Selected: False
Id: 5, AppId: 1005, AppName: Application 5, Selected: False
Id: 7, AppId: 1007, AppName: Application 7, Selected: False
Id: 9, AppId: 1009, AppName: Application 9, Selected: False

希望有帮助。