简体   繁体   English

更改某些用户的团队

[英]Change the teams of some users

I have a dictionary that keeps a team name ( string ) and the users of the team ( list ). 我有一个字典,保留团队名称( 字符串 )和团队用户( 列表 )。 I want to change the teams of some users. 我想改变一些用户的团队。

My idea is to find the place of the user who we want to move and remove it from the current collection. 我的想法是找到我们想要移动的用户的位置并将其从当前集合中删除 Then add the user to the desired team . 然后用户添加所需的团队 The problem is I can't find the given user place in the dictionary, thus I am not able to solve it. 问题是我无法在字典中找到给定的用户位置,因此我无法解决它。

var dictionary = new Dictionary<string, List<string>>();

dictionary.Add("Lighter", new List<string>() { "Royal", "JSmith" });
dictionary.Add("Darker", new List<string>() { "DCay", "Rebecca" });

string userToMove = "DCay";
string whereToMove = "Lighter";;
// expected results: 
// Lighter: Royal, JSmith, DCay
// Darker: Rebecca

I did the best I could for this post and I hope it is better than my old ones. 我为这篇文章做了最好的工作,我希望它比我的旧帖更好。

Full code with my comments: 完整代码与我的意见:

var dictionary = new Dictionary<string, List<string>>();

dictionary.Add("Lighter", new List<string>() { "Royal", "JSmith" });
dictionary.Add("Darker", new List<string>() { "DCay", "Rebecca" });

string userToMove = "DCay";
string whereToMove = "Lighter";

var list = dictionary.Values.SingleOrDefault(l => l.Contains(userToMove));  // list with user name
list?.Remove(userToMove);  // remove user from current list if user in list
dictionary[whereToMove].Add(userToMove); // add user to new list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM