views:

422

answers:

2

Ladys and Gents,

Im trying to filter out enteties from a principalcollection using what I think is called lamda 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

A: 

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!

martani_net
+1  A: 

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