07-10-2020 07:00 AM
I know this has to be something simple, but the following code always returns false. I'm literally looping through the current users groups and it tells me that they aren't in them.
foreach(UserGroup grp in app.CurrentUser.GetUserGroups())
{
app.Diagnostics.WriteIf(Diagnostics.DiagnosticsLevel.Verbose, app.CurrentUser.Name + " is in " + grp.Name + ": " + app.CurrentUser.GetUserGroups().Contains(grp).ToString());
}
07-10-2020 07:59 AM
I don't think most Unity API objects have equality operators defined so when you obtain the User Group object in two separate ways, from a .NET perspective they are not equal. The Contain method can take in an IEqualityComparer<T> as the second parameter, but I think that requires creating your own class to check for equality (at least I don't know any shortcut way to use it). So you could do that, or you could also use some other method that let's you pass in a predicate. In some cases they don't always return a boolean so you could check for the expected outcome. For example if Find() returns null, or Count > 0. The Any() extension method may be most similar to what you are looking for.
Here are the code examples that are all similar in returning a boolean, where the first one is the Contain method that will not give you the expected results, but the others should:
app.CurrentUser.GetUserGroups().Contains(grp)app.CurrentUser.GetUserGroups().Find(ug => ug.ID == grp.ID) != nullapp.CurrentUser.GetUserGroups().Count(ug => ug.ID == grp.ID) > 0app.CurrentUser.GetUserGroups().Any(ug => ug.ID == grp.ID)
07-10-2020 07:41 AM
Change
app.CurrentUser.GetUserGroups().Contains(grp).ToString()
To
app.CurrentUser.GetUserGroups().Contains(grp.Name)
07-10-2020 08:06 AM
I did try hard-coding a group name in, but the function is looking for a UserGroup object
07-10-2020 07:59 AM
I don't think most Unity API objects have equality operators defined so when you obtain the User Group object in two separate ways, from a .NET perspective they are not equal. The Contain method can take in an IEqualityComparer<T> as the second parameter, but I think that requires creating your own class to check for equality (at least I don't know any shortcut way to use it). So you could do that, or you could also use some other method that let's you pass in a predicate. In some cases they don't always return a boolean so you could check for the expected outcome. For example if Find() returns null, or Count > 0. The Any() extension method may be most similar to what you are looking for.
Here are the code examples that are all similar in returning a boolean, where the first one is the Contain method that will not give you the expected results, but the others should:
app.CurrentUser.GetUserGroups().Contains(grp)app.CurrentUser.GetUserGroups().Find(ug => ug.ID == grp.ID) != nullapp.CurrentUser.GetUserGroups().Count(ug => ug.ID == grp.ID) > 0app.CurrentUser.GetUserGroups().Any(ug => ug.ID == grp.ID)
07-10-2020 08:35 AM
The find method did the trick. Thanks!
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.