1

I have two IQueryable, wsSelectedApps and allApps. I need to loop through each app in allApps and check if property app.AppID exists in my other IQueryable.

My code so far is....

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

The IF statement originally had

If wsSelectedApps.Contaings(app.AppID) Then...

This was fine when wsSelectedApps was just a list of AppIDs but it now contains two properties. How do I check if the app.AppID exists in wsSelectedApps?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mych
  • 2,527
  • 4
  • 36
  • 65
  • Your question is very difficult to understand - there's a lot of room for interpretation. Try to clarify your question. Give examples. Expected inputs and outputs. Object models. Whatever it takes to make things easy to understand. – Sam Axe Oct 15 '19 at 17:05

3 Answers3

2

You can use the Any() extension provided by System.LINQ.
Your if condition would read as follows

For Each app In allApps
    If wsSelectedApps.Any(Function(selected) selected.AppID = app.AppID) Then
        'Do something
    Else
        'Do something else
    End if
Next
Anu6is
  • 2,137
  • 2
  • 7
  • 17
2

You can achieve this using a left outer join in LINQ. This is similar to SQL, but with LINQ you can specify a default value when a match is not found. I hope this works for you - I did as much as I can with limited knowledge of your database schema.

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

See this answer for more about vb.net LINQ left outer join.

djv
  • 15,168
  • 7
  • 48
  • 72
1

You could use a shortcut and create True, False lists directly from the source lists as follow:

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

Unless you need to create new list of the DeploymentModel object, you could modify the properties of the DeploymentModel objects as follow:

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

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

And, below the output of a quick test:

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

Hope it helps.

  • 1
    You should probably include your `ForEach` [extension method](https://stackoverflow.com/q/101265/832052) to make this complete – djv Oct 15 '19 at 17:53
  • Sure, I'll fix it. –  Oct 15 '19 at 17:56
  • I would handle the problem a similar way as this (I have a ForEach of my own...) in making two lists. You may even be able to include the `Selected` property and the ID / 0 in the select. I was thinking of using `JOIN` but there are already two good answers here including this one! – djv Oct 15 '19 at 17:56
  • I posted an answer too. For yours, I am referring to the fact that `ForEach` is not a member of IEnumerable / IQueryable, and that many people to use it, but it must be in an extension method. If you navigate to definition, you will see that it's not in .NET framework. – djv Oct 15 '19 at 18:40