开发者

C# Linq String[] list minus from String[] list

开发者 https://www.devze.com 2023-01-11 16:35 出处:网络
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.

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开发者_Python百科?


// 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 );


users.Except(admins);

See more set operations:

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


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

var nonAdmins = users.Except(admins);
0

精彩评论

暂无评论...
验证码 换一张
取 消