如何使用匿名函数 (lambda) 复制此代码?
How can I replicate this code by using anonymous function (lambda)?
我有一个嵌套字典,如下所示:
Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>();
第一个字符串是用户名,第二个是他参加的比赛,int是他的分数。一个用户可以参加多个比赛。
我的任务是通过添加他拥有的所有点数来找到得分最高的用户。现在我使用这个代码:
foreach (var user in users)
{
bestUsers.Add(user.Key, 0);
foreach (var contest in user.Value)
{
bestUsers[user.Key] += contest.Value;
}
}
我想知道如何使用类似如下所示的匿名函数来做到这一点:
KeyValuePair<string, int> bestUser = users.OrderBy(x => x.Value.Sum());
您可以创建代表用户结果的 class 而不是嵌套字典:
public class UserGameResults
{
public string Name { get; set; } // the name of the user
public int TotalScore { get => GameResults.Select(x => x.Value).Sum(); } // total score of all games, will be calculated every time the property is accessed
public Dictionary<string,int> GameResults { get; set; } = new Dictionary<string,int>(); // key is the name of the game, value is the score
}
如果您使用 Dictionary<string,UserGameResults>
,您将更容易获得结果:
var bestResult = users.OrderByDescending(x => x.Value.TotalScore).FirstOrDefault();
此外,Dictionary<string,UserGameResults>
比 Dictionary<string,Dictionary<string,int>>
更能告诉您数据的含义。
对于使用 linq 获取字典而不是 2 个 foreach 循环的代码重构,您可以使用如下内容:
users.ToDictionary(u => u.Key, u => u.Value.Select(c => c.Value).Sum());
或者我认为 Sum 采用了选择器 lambda
users.ToDictionary(u => u.Key, u => u.Value.Sum(c => c.Value));
应该有效
当您需要存储值和一些与它们关联的唯一键时,您可以使用 Dictionary<TKey,TValue>
,并且通过该键访问它们对您来说很方便。
我不知道你为什么在这里使用字典。我认为您的用户名不能是唯一的。因此,如果用户名不是唯一的,那么您如何将所有用户存储在字典中。我认为你应该使用列表而不是字典。
Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>()
{
{ "A", new Dictionary<string, int>(){
{"sub1", 10},
{"sub2", 20},
{"sub3", 30}
}
},
{ "B", new Dictionary<string, int>(){
{"sub1", 10},
{"sub2", 40},
{"sub3", 30}
}
}
};
var result = users.OrderBy(x => x.Key).ToDictionary(m => m.Key, m => m.Value.Sum(k => k.Value));
Dictionary 速度非常快,经过优化,在大多数情况下都能提供您所需的性能。但在大多数情况下这并不重要,键值对链表的性能也足够了。
试试这个
var dict = new Dictionary<string, Dictionary<string, int>> {
{ "name1", new Dictionary<string, int>{ { "A", 2 }, {"B", 3 }}},
{ "name2", new Dictionary<string, int>{ { "C", 4 }, {"D", 5 }}}
};
var scores = dict.Select(d => new { name = d.Key, score = d.Value.Select(x => x.Value).Sum() } )
.ToList().OrderByDescending (d =>d.score );
分数
name2 9
name1 5
我有一个嵌套字典,如下所示:
Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>();
第一个字符串是用户名,第二个是他参加的比赛,int是他的分数。一个用户可以参加多个比赛。
我的任务是通过添加他拥有的所有点数来找到得分最高的用户。现在我使用这个代码:
foreach (var user in users)
{
bestUsers.Add(user.Key, 0);
foreach (var contest in user.Value)
{
bestUsers[user.Key] += contest.Value;
}
}
我想知道如何使用类似如下所示的匿名函数来做到这一点:
KeyValuePair<string, int> bestUser = users.OrderBy(x => x.Value.Sum());
您可以创建代表用户结果的 class 而不是嵌套字典:
public class UserGameResults
{
public string Name { get; set; } // the name of the user
public int TotalScore { get => GameResults.Select(x => x.Value).Sum(); } // total score of all games, will be calculated every time the property is accessed
public Dictionary<string,int> GameResults { get; set; } = new Dictionary<string,int>(); // key is the name of the game, value is the score
}
如果您使用 Dictionary<string,UserGameResults>
,您将更容易获得结果:
var bestResult = users.OrderByDescending(x => x.Value.TotalScore).FirstOrDefault();
此外,Dictionary<string,UserGameResults>
比 Dictionary<string,Dictionary<string,int>>
更能告诉您数据的含义。
对于使用 linq 获取字典而不是 2 个 foreach 循环的代码重构,您可以使用如下内容:
users.ToDictionary(u => u.Key, u => u.Value.Select(c => c.Value).Sum());
或者我认为 Sum 采用了选择器 lambda
users.ToDictionary(u => u.Key, u => u.Value.Sum(c => c.Value));
应该有效
当您需要存储值和一些与它们关联的唯一键时,您可以使用 Dictionary<TKey,TValue>
,并且通过该键访问它们对您来说很方便。
我不知道你为什么在这里使用字典。我认为您的用户名不能是唯一的。因此,如果用户名不是唯一的,那么您如何将所有用户存储在字典中。我认为你应该使用列表而不是字典。
Dictionary<string, Dictionary<string, int>> users = new Dictionary<string, Dictionary<string, int>>()
{
{ "A", new Dictionary<string, int>(){
{"sub1", 10},
{"sub2", 20},
{"sub3", 30}
}
},
{ "B", new Dictionary<string, int>(){
{"sub1", 10},
{"sub2", 40},
{"sub3", 30}
}
}
};
var result = users.OrderBy(x => x.Key).ToDictionary(m => m.Key, m => m.Value.Sum(k => k.Value));
Dictionary 速度非常快,经过优化,在大多数情况下都能提供您所需的性能。但在大多数情况下这并不重要,键值对链表的性能也足够了。
试试这个
var dict = new Dictionary<string, Dictionary<string, int>> {
{ "name1", new Dictionary<string, int>{ { "A", 2 }, {"B", 3 }}},
{ "name2", new Dictionary<string, int>{ { "C", 4 }, {"D", 5 }}}
};
var scores = dict.Select(d => new { name = d.Key, score = d.Value.Select(x => x.Value).Sum() } )
.ToList().OrderByDescending (d =>d.score );
分数
name2 9
name1 5