[英]Change the teams of some users
我有一個字典,保留團隊名稱( 字符串 )和團隊用戶( 列表 )。 我想改變一些用戶的團隊。
我的想法是找到我們想要移動的用戶的位置 , 並將其從當前集合中刪除 。 然后將用戶添加到所需的團隊 。 問題是我無法在字典中找到給定的用戶位置,因此我無法解決它。
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
我為這篇文章做了最好的工作,我希望它比我的舊帖更好。
完整代碼與我的意見:
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.