c# - 在 MessageBox 中显示结果
问题描述
有 3 个最常出现的单词的代码
string words = "One one Two two three four".ToLower();
var results = words
.Split(' ')
.Where(x => x.Length > 3)
.GroupBy(x => x)
.Select(x => new {
Count = x.Count(),
Word = x.Key })
.OrderByDescending(x => x.Count)
.Take(3);
foreach (var item in results)
{
MessageBox.Show(String.Format("{0} occurred {1} times", item.Word, item.Count));
}
它可以工作,但我想按一次按钮并在 MessageBox 中显示所有结果,如下所示:
解决方案
加入字符串,然后只显示一次消息框:
MessageBox.Show(String.Join("\n", results.Select(x => String.Format("{0} occurred {1} times", x.Word, x.Count)));
或使用字符串插值:
MessageBox.Show(String.Join("\n",
results.Select(x => $"{x.Word} occurred {x.Count} times"));
推荐阅读
- php - 您将如何向模态显示数据?
- c# - ASP.Net Core 3.0 依赖注入忽略工厂方法?
- r - dplyr 将列列表传递给函数以计算缺失
- c# - 在 UWP 中将上下文菜单项添加到 Windows 资源管理器的文件上下文中
- python-3.x - 如何在 Jupyter Lab 中使用 Altair 渲染对象?
- shell - linux bash命令hwclock输出含义
- c# - 在 C# Identity Insert 中插入 Edit Windows Forms 数据库设置为 Off
- python - 有没有办法我可以将列表作为python中的单个变量读取
- python - 如何在python中调用带参数的存储过程?
- ios - 使用按钮快速循环遍历数组项