views:

16

answers:

1

Hello, I have run into a problem where I need to remove certain items from collections that seem to only have get all functions.

My Idea is to get the full collection and then remove the unneeded items. Then take that new collection and bind it to whatever I need.

For example How do I get all the roles except administrator?

Roles strRoles = Roles.GetAllRoles()
RoleList.DataSource = (Roles) roles; //Roles.GetAllRoles();
RoleList.DataBind();

or How do I get all users but say user123

MembershipUserCollection users = Membership.GetAllUsers();
UserList.DataSource = users;
UserList.DataBind();

Thanks in advance, -Scott

+1  A: 

You can use the Where LINQ extension method to accomplish this. LINQ extension methods operate on collections that implement the IEnumerable<> interface. For your first example you could do the following:

RoleList.DataSource = Roles.GetAllRoles().Where(r => !r.Equals("Administrator"))

For your second:

IEnumerable<MembershipUser> users = Membership.GetAllUsers().Cast<MembershipUser>()
UserList.DataSource = users.Where(m => !m.UserName.Equals("123"));

Calling the Cast method first will convert the collection to an IEnumerable of Membershipusers.

sgriffinusa
Thank you, This worked perfect.
Scott