How to port this Linq to VS 2005

Go To


I have following code in VS2008

If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then
    RoleCheckBox.Checked = True
    RoleCheckBox.Checked = False
End If

I need the above code in VS2005

What can i do instead of linq in above code? Can anybody help?

2009-06-16 09:12
by user42348
Can you give us some more context please, e.g. What type is selectedUsersRoles (i.e. what's the question mark here "Dim selectedUsersRoles as ?"), thanks - Binary Worrier 2009-06-16 09:20


RoleCheckBox.Checked = False
For Each str As String in selectedUsersRoles
     If str = RoleCheckBox.Text Then
          RoleCheckBox.Checked = True
          Exit For
     End If

If you don't wish to alter the RoleCheckBox.Checked twice (when str is actually found) then declare a boolean flag (i.e. boolFound) with an initial False value, change it to true when found, and asign RoleCheckBox.Checked = boolFound after the "for each" loop....

2009-06-16 09:36
by Rodolfo G.


bool containsRole = false;
foreach(string entry in selectedUsersRoles)
  if(entry == RoleCheckBox.Text)
    containsRole = true;

RoleCheckBox.Checked = containsRole;

The code is C# but i guess u'll get the idea. This is for IEnumerable. If you have a list try Binary Worrier's sollution.

2009-06-16 09:20
by Adrian Zanescu