This project is read-only.

Exclude Administrators from query

Topics: Customizing Orchard
Apr 14, 2015 at 7:20 PM
Edited Apr 15, 2015 at 8:02 PM
How would I exclude users with the administrator role from the following query (query is in the AdminController.cs file of Orchard.Users module)?

var users = Services.ContentManager.Query<UserPart, UserPartRecord>();
The best I've come up with is the following, but it fails somewhere in the projections due to an int/string mismatch.

var users = Services.ContentManager.Query<UserPart, UserPartRecord>().Where(x => !Services.ContentManager.Get(x.Id).Get<UserRolesPart>().Roles.Contains("Administrator"));
Thank you.

Edit: The exception occurs in DefaultContentQuery.cs on line 173
Apr 16, 2015 at 5:00 PM
I managed to accomplish this but had to create a separate list of usernames for those who have the administrator privilege and then do the query as follows:
var allUsers = Services.ContentManager
                .Query<UserPart, UserPartRecord>().Where(x => x.UserName != _siteService.GetSiteSettings().SuperUser).Where(x => !administrators.Contains(x.UserName));
Marked as answer by emeraldarcher on 4/16/2015 at 9:00 AM