tags:

views:

115

answers:

3

Hello, Let's say

string[] admins = "aron, mike, bob";
string[] users = "mike, katie, sarah";

How can I take the users and strip out anyone from the admins.

the result should be "katie, sarah"; (mike was removed)

Is there a nice Linq way to do this?

+2  A: 
users.Except(admins);

See more set operations:

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Ian Henry
+2  A: 

The easiest would to be use IEnumerable<T>.Except:

var nonAdmins = users.Except(admins);
Justin Niessner
+3  A: 
// as you may know, this is the right method to declare arrays       
string[] admins = {"aron", "mike", "bob"};
string[] users = {"mike", "katie", "sarah"};

// use "Except"
var exceptAdmins = users.Except( admins );
codemeit
Actually, that's not right either: `string[] admins = {"aron", "mike", "bob"}; `
James Curran
Thanks James, you are right. the quotes have been added.
codemeit
This:string[] customers = Roles.GetUsersInRole("customer"); string[] admins = Roles.GetUsersInRole("admin"); string[] result = customers.Except(customers, admins); gives me this error:The type arguments for method 'System.Linq.Enumerable.Except<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Collections.Generic.IEnumerable<TSource>, System.Collections.Generic.IEqualityComparer<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
aron