How would you suggest to in C# all users in GrpA and Grp B stored them in GrpAB using List<string>
This is my current solution. Any better search algorithms?
public void membersOfBothGroup(string groupA,string groupB) { List usersInGroupA = getGroupMembers(groupA); List usersInGroupB = getGroupMembers(groupB); List userInBothAB = new List(); foreach (string userA in usersInGroupA) { foreach(string userB in usersInGroupB) { 开发者_如何学Go if (userA == userB) { userInBothAB.Add(userA); } } } }
Here's an example (since the datatype is string
):
List<string> groupA = new List<string>() { "Moe", "Larry", "Curly" };
List<string> groupB = new List<string>() { "Moe", "Shemp", "CurlyJoe" };
var result = groupB.Intersect(groupA);
Output:
Moe
How about:
List<string> userinBothAB = usersInGroupA.Intersect(usersInGroupB);
Check the MSDN for more details.
精彩评论