c# - 一起添加列表

标签 c#

我有多个字符串列表,IList<string> ,我想将其合并到一个列表中,显示不同的字符串并为每个项目计数(如字典)。最有效的方法是什么?

最佳答案

LINQ(在代码类型和维护方面肯定是最高效的;整体性能将大约与任何其他方法相同):

如果列表在单独的变量中:

var qry = from s in listA.Concat(listB).Concat(listC) // etc
          group s by s into tmp
          select new { Item = tmp.Key, Count = tmp.Count() };

如果列表都在(列表的)父列表中:

var qry = from list in lists
          from s in list
          group s by s into tmp
          select new { Item = tmp.Key, Count = tmp.Count() };

那么如果你真的想要一个列表:

var resultList = qry.ToList();

关于c# - 一起添加列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/467467/

相关文章:

c# - 在数据库中存储和解析 bool 表达式

c# - 如何在 C# 中将 "import"设置为静态类?

c# - 通过 C# 连接 Arduino USB

c# - Instagram 基本显示 API : RestSharp Invalid authorization code response

c# - 我应该在自己的线程中运行每个插件吗?

c# - 在 GoDaddy 上托管 selenium 应用程序。 "This program is blocked by group policy"

c# - Iframe 从 C# 中的代码隐藏自动调整高度

c# - 通过 ActionLink 提交 Ajax.BeginForm

c# - 复制并粘贴到DataGridView单元格(C#)

c# - 此 XPath 的 LINQ to XML 等效项是什么