遍历对象A的IQueriable,并检查属性是否存在于对象I的另一个IQueriable中]]

问题描述 投票:1回答:3

我有两个IQueriable,wsSelectedAppsallApps。我需要遍历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) Then...

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

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

vb.net iqueryable
3个回答
2
投票

您可以使用Any()提供的System.LINQ扩展名。您的if条件如下:

For Each app In allApps
    If wsSelectedApps.Any(Function(selected) selected.AppID = app.AppID) Then
        'Do something
    Else
        'Do something else
    End if
Next

2
投票

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


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
© www.soinside.com 2019 - 2024. All rights reserved.