Simon Olsen
Simon Olsen

Reputation:

PrincipalSearchResult<Principal>, Lambda Expressions

I'm trying to filter out entities from a principalcollection using what I think is called lambda expressions.

I cant get this to work, I get no results. user.GetGroups() returns all the groups where user is member, but user.GetGroups().Where(....) does not return anything.

Lets say that userprincipal user = Administrator
Lets say MyFilterString = "Exchange"
Lets say that Administrator is member of the following groups:

Exchange Domain Servers
Exchange Services
Administrators
Enterprise Admins

Domain Admins Schema Admins


UserPrincipal user = new UserPrincipal(MyActiveDirectoryContext);
..
..
PrincipalSearchResult<Principal> memberOfGroups = user.GetGroups().Where(g => g.SamAccountName.Contains(MyFilterString) == true) as PrincipalSearchResult<Principal>;

What I expect is that memberOfGroups should now contain 2 groups:

Exchange Domain Servers
Exchange Services

But it ends up empty, zip zero, nada. A little help is very much appreciated.

Simon O. Olsen

Upvotes: 1

Views: 3884

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063774

If .Where is the standard Enumerable.Where, then it isn't going to ever return a PrincipalSearchResult; so as will return null every time. Consider using .ToList() :

var memberOfGroups = user.GetGroups()
      .Where(g => g.SamAccountName.Contains(MyFilterString)).ToList()

Upvotes: 2

0xFF
0xFF

Reputation: 4178

I guess you should use IndexOf() and not Contains() like this:

PrincipalSearchResult memberOfGroups = user.GetGroups().Where(g => >g.SamAccountName.IndexOf(MyFilterString) > -1) as PrincipalSearchResult;

you may want to reformulate your question to be more clearer!

Upvotes: 0

Related Questions